Need Help

This is a discussion on Need Help within the C++ Programming forums, part of the General Programming Boards category; #include <iostream.h> #include <stdlib.h> int main() { int x; int y; int entry; cout <<"///////////// /Easy Math Calculator ///////////////"<< endl; ...

  1. #1
    Registered User
    Join Date
    Oct 2001
    Posts
    55

    Need Help

    #include <iostream.h>
    #include <stdlib.h>
    int main()
    {

    int x;
    int y;
    int entry;


    cout <<"///////////// /Easy Math Calculator ///////////////"<< endl;
    cout <<" david4050@hotmail.com\n\n";
    cout <<"Hey! You want to see this program do your own simple math quotation?"<< endl;
    cout <<"Enter a number and press Enter.."<< endl;

    cin >> x;

    cout <<"The number you just entered was, " << x << endl;

    cout <<"Please Enter another number and press Enter..."<< endl;

    cin >> y;

    cout <<"Do you want the Program do devide your numbers, multiply, add or subtract?\n\n";
    cout <<" Press M and Enter for multiply\n\n";
    cout <<" Press D and Enter for Devide\n\n";
    cout <<" Press S and Enter for Subtraction\n\n";
    cout <<" Press A and Enter for Addition\n\n";
    cin >> entry;

    if (entry = "A")
    {
    cout <<"Your Numbers Have been...ADDED!\n\n";
    }

    system("PAUSE");

    return 0;
    }



    I need help cause on line if (entry = "A") it sais it lacks a cast...and i have no idea what that means. All i want it to do is to ask the person if the want there numbers added, multiplyed, devided or subtracted..and then the if (entry = "A") thing is for addition so it would write out "Your numbers have been..ADDED!" can anyone help me with why this isnt working?
    D4050
    Glorified C++ Programmer
    Console, DOS, HTML, Javascript, Visual Basics Compatible

  2. #2
    Lead Moderator kermi3's Avatar
    Join Date
    Aug 1998
    Posts
    2,595

    my favorite little error...

    <<if (entry = "A")>>

    that's what you have...just one little thing that ruins huge programs...

    if (entry == "A")

    if you still have problems with the line after that try putting the 'A' in single quotes like that so its...

    if (entry == 'A')
    Kermi3

    If you're new to the boards, welcome and reading this will help you get started.
    Information on code tags may be found here

    - Sandlot is the highest form of sport.

  3. #3
    Registered User
    Join Date
    Aug 2001
    Posts
    61
    Change

    int entry;

    to

    char entry;

    because int entry can only hold numbers and char entry can hold letters.

  4. #4
    Registered User
    Join Date
    Oct 2001
    Posts
    55
    ya baby it works thanks dude. Im 12..so its a bit confusin heh
    D4050
    Glorified C++ Programmer
    Console, DOS, HTML, Javascript, Visual Basics Compatible

  5. #5
    Lead Moderator kermi3's Avatar
    Join Date
    Aug 1998
    Posts
    2,595
    if you used single quotes though wouldn't int work too? I'd just take the ASCII value....
    Kermi3

    If you're new to the boards, welcome and reading this will help you get started.
    Information on code tags may be found here

    - Sandlot is the highest form of sport.

  6. #6
    Registered User
    Join Date
    Oct 2001
    Posts
    55
    actually, the single quotes were the only thing that DID work properly. but now with the...

    if (entry == 'A')
    {
    cout <<"Your numbers added are: /n/n"<<endl;
    }
    else if (entry == 'S')
    {
    cout <<"Your numbers subtracted are: /n/n"<<endl;
    }
    else if (entry == 'D')
    {
    cout <<"Your numbers divided are: /n/n"<<endl;
    }
    else if (entry == 'M')
    {
    cout <<"Your numbers multiplied are: /n/n"<<endl;
    }
    else
    {
    cout <<"Sorry, the key you entered was invalid. Please try again!"<<endl;
    }



    it aint comin out right. cuz when i enter A in the box instead of saying "Your numbers multiplied are: " it sais "Sorry, the key you entered was invalid. Please try again."
    D4050
    Glorified C++ Programmer
    Console, DOS, HTML, Javascript, Visual Basics Compatible

  7. #7
    Lead Moderator kermi3's Avatar
    Join Date
    Aug 1998
    Posts
    2,595
    hmmm I have to look at it some more but for something like this see if your teacher will let you use a switch...it's a little easier for some things, though not many, but a menu is one of those things.

    Are you sure you're entering a capital 'A' instead of 'a'? That may be the problem, it's case sensitive.
    Kermi3

    If you're new to the boards, welcome and reading this will help you get started.
    Information on code tags may be found here

    - Sandlot is the highest form of sport.

  8. #8
    Registered User
    Join Date
    Oct 2001
    Posts
    55
    heh..yea right.....i dont even have a teacher. its not homework.im just learnin it seperatly..at my skool this is grade 12 work. im in grade 7..they wont even let me TOUCH html books..even though i know it back to front. sad eh? wut is a switch????
    D4050
    Glorified C++ Programmer
    Console, DOS, HTML, Javascript, Visual Basics Compatible

  9. #9
    Registered User
    Join Date
    Aug 2001
    Posts
    61
    Make sure that when you press "A" in your program that its a capital letter because the statement

    if (entry == 'A')

    only checks to see if you've pressed a capital "A".

  10. #10
    Registered User
    Join Date
    Oct 2001
    Posts
    55
    Yea i did..same problem . is there something wrong with the IF statements? i only know how to use them from javascript and read like haf a line for c++ and looked at some code so i guessed that u just had to add brackets....Eg:

    If (userEntry = 1)
    {
    cout<<"You entered 1!"endl;
    }
    else
    {
    cout<<"Invalid"endl;
    }
    return 0;
    }

    type of thing..am i on the right track? is my code?
    D4050
    Glorified C++ Programmer
    Console, DOS, HTML, Javascript, Visual Basics Compatible

  11. #11
    Lead Moderator kermi3's Avatar
    Join Date
    Aug 1998
    Posts
    2,595
    ok here's how a switch works...its been a while so if do some syntax wrong someone tell me hehe.


    ok you would do:

    switch(entry)
    {
    case 'A': //basically a glorified if
    {
    cout << "blah";
    break; //you have to have this break at the end of things...here's what would happen without a break, sometimes you wouldn't want it
    }

    case 'M':
    case 'm':
    {
    cout << "foo";
    break;
    }
    //because there's no break between 'M' and 'm' it will do it for both...the problem comes in here

    default:
    {
    cout << "not valid";
    }


    //put default this at the bottom of the switch and it will
    // always do what's in default if its still in teh switch
    //when it gets there...very useful
    }


    //if you don't put a break at the end of things...it will keep going in the switch, so if there wasn't one in A then it would cout blah from A and then "not valid" from the default.
    Kermi3

    If you're new to the boards, welcome and reading this will help you get started.
    Information on code tags may be found here

    - Sandlot is the highest form of sport.

  12. #12
    Registered User
    Join Date
    Oct 2001
    Posts
    55
    hmmmm i get it now i get it seems to me switchs are much more powerful and easier to handle. alright ill give it a go..
    D4050
    Glorified C++ Programmer
    Console, DOS, HTML, Javascript, Visual Basics Compatible

  13. #13
    Lead Moderator kermi3's Avatar
    Join Date
    Aug 1998
    Posts
    2,595
    switches get ugly in some more advanced stuff (I'm told I've never actually found a time), but certain times, like menu especailly, there're very good. I much prefer ifs....less to go wrong, except for menu's most the time...
    Kermi3

    If you're new to the boards, welcome and reading this will help you get started.
    Information on code tags may be found here

    - Sandlot is the highest form of sport.

  14. #14
    Registered User
    Join Date
    Oct 2001
    Posts
    55
    k this is the code..its saying that case 'D' is not in a switch statement....any suggestions?



    switch(entry)
    case 'm':
    case 'M':
    {
    cout<<"Your numbers multiplied are:"<<endl;
    break;
    }
    case 'D':
    case 'd':
    {
    cout<<"Your numbers devided are:"<<endl;
    }
    break;
    }
    case 'S':
    case 's':
    cout<<"Your numbers subtracted are:"<<endl;
    }
    break;
    }
    case 'A':
    case 'a':
    cout<<"Your numbers added are:"<<endl;
    }
    break;
    }
    default:
    {
    cout<<"Sorry, Invalid character. Please retry!"<<endl;
    }
    D4050
    Glorified C++ Programmer
    Console, DOS, HTML, Javascript, Visual Basics Compatible

  15. #15
    Mayor of Awesometown Govtcheez's Avatar
    Join Date
    Aug 2001
    Location
    MI
    Posts
    8,825
    Code:
    switch(entry) 
    {
       case 'm': 
       case 'M': 
       { 
          cout<<"Your numbers multiplied are:"<<endl; 
          break; 
       } 
       case 'D': 
       case 'd': 
       { 
         cout<<"Your numbers devided are:"<<endl; 
          break; 
       }
       case 'S': 
       case 's': 
       {
          cout<<"Your numbers subtracted are:"<<endl; 
          break; 
       } 
       case 'A': 
       case 'a': 
       {
           cout<<"Your numbers added are:"<<endl; 
           break; 
       } 
       default: 
       { 
            cout<<"Sorry, Invalid character. Please retry!"<<endl; 
       }
    }
    Try that - your chicken lips (curly braces) were badly off

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21