Puppy Linux Discussion Forum Forum Index Puppy Linux Discussion Forum
Puppy HOME page : puppylinux.com
"THE" alternative forum : puppylinux.info
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

The time now is Wed 22 Oct 2014, 11:30
All times are UTC - 4
 Forum index » Off-Topic Area » Programming
Rebuilding expr
Post new topic   Reply to topic View previous topic :: View next topic
Page 1 of 1 [7 Posts]  
Author Message
thelaptopkiller

Joined: 25 Oct 2009
Posts: 67
Location: The only place in tornado ally with no tornadoes

PostPosted: Sat 14 Aug 2010, 13:02    Post subject:  Rebuilding expr
Subject description: working on adding floating point numbers
 

I'm working on learning C++.
As my first real test I wanted to build a better expr.
But it doesn't seem to do any adding or print the answer.

Here is the code:
Code:
#include <iostream>
#include <cstdlib>

using namespace std;

int main(int argc, char* argv[])
{

  int proarg;
  float fltAnswer;
  if ( argc != 4 )
    cout << "Usage: expr number1 +-/* number2";
  else

     float arg1 = atof(argv[2]);
     float arg2 = atof(argv[4]);
     if (argv[3] == "/")
     proarg = 1;
     else if (argv[3] == "*")
     proarg = 2;
     else if (argv[3] == "+")
     proarg = 3;
     else if (argv[3] == "-")
     proarg = 4;

    switch (proarg)
     {
        case 1 :
            fltAnswer = (float) arg1 / arg2;
            cout << fltAnswer;
            break;
        case 2 :
            fltAnswer = (float) arg1 * arg1;
            cout << fltAnswer;
            break;
        case 3 :
            fltAnswer = (float) arg1 + arg2;
            cout << fltAnswer;
            break;
        case 4 :
            fltAnswer = (float) arg1 - arg1;
            cout << arg1 << "+" << arg2 << fltAnswer;
            break;
        }
}


I'm building it with Code::Blocks ver. 8.2

Help anyone?

_________________

Really,you wanna mess with me?
Back to top
View user's profile Send private message 
muggins

Joined: 20 Jan 2006
Posts: 6688
Location: lisbon

PostPosted: Sun 15 Aug 2010, 10:55    Post subject:  

Hello thelaptopkiller,

I'm nearly there. I altered your code to:

Code:
#include <iostream>
#include <cstdlib>

using namespace std;

int main(int argc, char* argv[])
{
   
  float fltAnswer;
  float arg1 = atof(argv[1]);
  float arg3 = atof(argv[3]);
 
   if ( argc != 4 )
   {   
     cout << "Usage: expr number1 +-/* number2";
     exit(1);
   }
   

    switch (argv[2][0])
    {
        case '/' :
            fltAnswer = (float) arg1 / arg3;
            cout << fltAnswer;
            break;
        case '*' :
            fltAnswer = (float) arg1 * arg3;
            cout << fltAnswer;
            break;
        case '+' :
            fltAnswer = (float) arg1 + arg3;
            cout << fltAnswer;
            break;
        case '-' :
            fltAnswer = (float) arg1 - arg3;
            cout << arg1 << "+" << arg3 << fltAnswer;
            break;
     
   }
     return 0;
}


The only thing not working is that it is that, for some reason, "6 * 4" is being seen as argc==4.

Two things regarding argv. If you call the program arithmetic, then run:

Code:
./arithmetic 2 + 3


argv[0] = ./arithmetic
argv[1] = 2
argv[2] = +
argv[3] = 3

And in parsing argv[2], for the switch statement, it is actually like an array, and the "+" is contained in the first element of this array, [2][0].

I'll see if I can work out why the "*" isn't working...unless someone else comes along with a solution.

Edit: The switch should be for [2][0].
Back to top
View user's profile Send private message 
muggins

Joined: 20 Jan 2006
Posts: 6688
Location: lisbon

PostPosted: Sun 15 Aug 2010, 11:18    Post subject:  

If you replace "*" with "x", as the multiplier operator, then the above code works with multiply. I wonder whether using * is seen as a pointer?
Back to top
View user's profile Send private message 
rcrsn51


Joined: 05 Sep 2006
Posts: 9201
Location: Stratford, Ontario

PostPosted: Sun 15 Aug 2010, 11:24    Post subject:  

Or maybe the * is being processed as a wild-card operator.
Back to top
View user's profile Send private message 
thelaptopkiller

Joined: 25 Oct 2009
Posts: 67
Location: The only place in tornado ally with no tornadoes

PostPosted: Sun 15 Aug 2010, 15:54    Post subject:  

I tried the new code and the program crashed.
You see as puppy linux dislikes my wifi card I'm stuck with good(much better then win 7) old win XP.
So when I run it window$ screams ERROR! ERROR! in my face.
Now I'm stuck with finding a way to make it window$ friendly.

Heres my new code:
Code:
#include <iostream>
#include <cstdlib>

using namespace std;

int main(int argc, char* argv[])
{
  float arg1 = atof(argv[2]),arg2 = atof(argv[4]),fltAnswer;

  if ( argc != 4 )
 {
    cout << "Usage: expr number1 +-/x number2";
    exit(1);
 }
  else
{

    switch (argv[2][0])
    {
        case '/' :
            fltAnswer = (float) arg1 / arg2;
            cout << fltAnswer;
            break;
        case 'x' :
            fltAnswer = (float) arg1 * arg2;
            cout << fltAnswer;
            break;
        case '+' :
            fltAnswer = (float) arg1 + arg2;
            cout << fltAnswer;
            break;
        case '-' :
            fltAnswer = (float) arg1 - arg2;
            cout  << fltAnswer;
            break;

   }
}
}

_________________

Really,you wanna mess with me?
Back to top
View user's profile Send private message 
Patriot


Joined: 15 Jan 2009
Posts: 734

PostPosted: Sun 15 Aug 2010, 18:26    Post subject:  

Hmmm .....

The '*' is a bash special character. Need to escape it :

# expr 3 \* 5


Rgds
Back to top
View user's profile Send private message 
muggins

Joined: 20 Jan 2006
Posts: 6688
Location: lisbon

PostPosted: Sun 15 Aug 2010, 20:12    Post subject:  

Thanks Patriot, that works, but for simplicity probably better to just use "x".

laptopkiller, have a look at your code, for handling arguments, again. Just as with Grub bootloader, where the first disk is (hd0,0) not (hd0,1), similarly when argv processes ./progname 6 / 7, the 6 is argv[1], / is argv[2] & 7 is argv[3].
Back to top
View user's profile Send private message 
Display posts from previous:   Sort by:   
Page 1 of 1 [7 Posts]  
Post new topic   Reply to topic View previous topic :: View next topic
 Forum index » Off-Topic Area » Programming
Jump to:  

You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Powered by phpBB © 2001, 2005 phpBB Group
[ Time: 0.0646s ][ Queries: 11 (0.0034s) ][ GZIP on ]