Thread: Please help--I'm a noob

  1. #1
    Registered User
    Join Date
    Mar 2013
    Posts
    4

    Smile Please help--I'm a noob

    So I'm just starting out with C++, and I thought I would try to program a simple calculator. My code seems good (to me ), but when I try to run it, I get two errors:
    1. It tells me "w" has not been defined, even thought I declared it in the do function
    2. When I run the program, subtraction kinda works, but I get crazy answers for everything else (ex. 1 + 1 = 98)
    Code:
    #include <iostream>
    using namespace std;
    
    
    int main (int x, int y, char z)
    {
        do {
            bool w = 1;
            cout << "\nTo run program, press 1, to exit press 0\n";
            cin>> w;
        char Input [3];
        cout << "Enter any operation.\n";
        cout << "Only one digit numbers are allowed.\n";
        cout << "Use * for multiplication,\n";
        cout <<    "/ for division,\n";
        cout <<    "+ for addition,\n";
        cout <<    "and -for subtraction.\n";
        cin>> Input;
        x = Input [0];
        y = Input [2];
        z = Input [1];
        switch (z)
        {
        case '+':
            cout << "And the answer is " << x+y << "\n";
            break;
        case '-':
            cout << "And the answer is " << x-y << "\n";
            break;
        case '*':
            cout << "And the answer is " << x*y << "\n";
            break;
        case '/':
            cout << "And the answer is " << x/y << "\n";
            break;
        default:
            break;
        }
        } while (w == 1);
    
    
        system ("pause");
    }
    No, I don't use comments.
    I don't like them.

    Thanks a million!

  2. #2
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,739
    0) Just a suggestion: You're better off using "false" and "true" instead of "0" and "1"
    00) main should be either "int main()" or "int main(int argc, char* argv[ ])"
    1)You declare "w" inside the do-while body, while the condition is considered as outside the do-while body.
    2) ASCII '1' has a value of 49 therefore '1'+'1' = 49+49 = 98

    EDIT: Oh, wait 'till you see a few eyesores, and believe me, you'll come to love comments!
    EDIT²: Don't mistake noob with newbie
    EDIT³: Your variable naming is awfully awful... Try putting meaningful names like my post #8
    Last edited by GReaper; 03-20-2013 at 06:07 PM.
    Devoted my life to programming...

  3. #3
    Registered User rogster001's Avatar
    Join Date
    Aug 2006
    Location
    Liverpool UK
    Posts
    1,472
    No, I don't use comments.
    I don't like them.
    That is a silly 'comment' to make Comments are essential, good code should speak for itself to the informed reader, but comments are expected as much as the code itself is. You have to illustrate certain things to yourself and other potential readers as you write your code. Not every single line, not every single declaration, just where it serves to indicate your train of thought and intention.

    I am not saying you have to include comments with code snippets you post here, but you need to be aware of their use in the wider context of your work
    Last edited by rogster001; 03-20-2013 at 05:30 PM.
    Thought for the day:
    "Are you sure your sanity chip is fully screwed in sir?" (Kryten)
    FLTK: "The most fun you can have with your clothes on."

    Stroustrup:
    "If I had thought of it and had some marketing sense every computer and just about any gadget would have had a little 'C++ Inside' sticker on it'"

  4. #4
    Registered User
    Join Date
    Mar 2013
    Posts
    4
    oh ok. Thanks, and I'll use comments with larger programs, just thought they were unnecessary for this. How can I make my program compute this right (Without 1+1=98)?
    Thanks
    Last edited by Purples; 03-20-2013 at 05:40 PM.

  5. #5
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,739
    Quote Originally Posted by Purples View Post
    How can I make my program compute this right (Without 1+1=98)?
    If you look at an ascii table, the numbers 0 to 9 are right next to each other, meaning they also have incremental values, with 0 being the smallest and 9 the highest. A simple formula would be to subtract the value of ASCII '0' from the user's input, like this:
    Code:
    number = input[0] - '0';
    Devoted my life to programming...

  6. #6
    Registered User rogster001's Avatar
    Join Date
    Aug 2006
    Location
    Liverpool UK
    Posts
    1,472
    cin input?? besides anything to do with ascii values only one of your array values is 'sort of ' initialised, so what would you expect the answers to be?
    Last edited by rogster001; 03-20-2013 at 05:54 PM.
    Thought for the day:
    "Are you sure your sanity chip is fully screwed in sir?" (Kryten)
    FLTK: "The most fun you can have with your clothes on."

    Stroustrup:
    "If I had thought of it and had some marketing sense every computer and just about any gadget would have had a little 'C++ Inside' sticker on it'"

  7. #7
    Registered User
    Join Date
    Mar 2013
    Posts
    4
    Worked great! Thanks a bunch.

  8. #8
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,739
    You're taking the long way though. You could just do:
    Code:
    int num1, num2;
    char oper;
    
    cin >> num1 >> oper >> num2;
    ( and maybe later check if they're lower than 10 or not )
    Devoted my life to programming...

  9. #9
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    >> char Input [3];
    >> cin>> Input;
    Do not ever do this. This is a fundamental security issue that can crash your computer (buffer overrun).
    You should not use char arrays in C++. If you need a string, then use std::string. Reading into a std::string is safe.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  10. #10
    Registered User
    Join Date
    May 2010
    Posts
    4,633
    Yes, prefer std::string over c-strings but this is also safe:
    Code:
     char Input [3];
    cin >> setw(3) >> Input;
    Jim

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A noob need help
    By ticktoc09 in forum C Programming
    Replies: 5
    Last Post: 02-26-2011, 02:37 AM
  2. Noob Help......If/Else :S
    By matt.s in forum C Programming
    Replies: 8
    Last Post: 03-20-2010, 03:37 PM
  3. noob.please help
    By sk8harddiefast in forum C Programming
    Replies: 13
    Last Post: 05-18-2009, 12:56 AM
  4. Noob needs some help
    By Tubby87 in forum C++ Programming
    Replies: 3
    Last Post: 03-06-2008, 05:11 AM
  5. plz help here, noob actually .D
    By BjoRn3n in forum C++ Programming
    Replies: 1
    Last Post: 03-07-2005, 03:04 PM