help!!

This is a discussion on help!! within the C++ Programming forums, part of the General Programming Boards category; Can anyone tell me what's wrong with this code please.. Code: #include <iostream> int main() { int a; int b; ...

  1. #1
    Registered User
    Join Date
    Oct 2005
    Posts
    5

    help!!

    Can anyone tell me what's wrong with this code please..
    Code:
    #include <iostream>
    int main()
    {
    	int a;
    	int b;
        int salary;
        cout<<"Enter hours worked (-1 to end):";
    	cin>>a;
    	cout<<"Enter hourly rate of the worker($00.00):";
    	cin>>b;
    	cout<<"Salary is"<<a*b)<<endl;
    	return(0);
    }

  2. #2
    Registered User
    Join Date
    Sep 2005
    Posts
    15
    y dont u try doing this:
    Code:
    #include <iostream>
    int main()
    {
    	int a;
    	int b;
        int salary;
        cout<<"Enter hours worked (-1 to end):";
    	cin>>a;
    	cout<<"Enter hourly rate of the worker($00.00):";
    	cin>>b;
    salary = a * b;
    	cout<<"Salary is "<< salary <<endl;
    system("PAUSE");
    	return(0);
    }

  3. #3
    Registered User
    Join Date
    Sep 2005
    Posts
    25
    I got it to work but

    warning C4101: 'salary' : unreferenced local variable

    Is one minor problem you might want to fix.

    Also remove the ")" from your program.

    Code:
    #include<iostream>
    
    using namespace std;
    
    int main()
    {
    int a;
    	int b;
        int salary;
        cout<<"Enter hours worked (-1 to end):";
    	cin>>a;
    	cout<<"Enter hourly rate of the worker($00.00):";
    	cin>>b;
    	cout<<"Salary is"<<a*b<<endl;
    	return(0);
    }

  4. #4
    Moderately Rabid Decrypt's Avatar
    Join Date
    Feb 2005
    Location
    Milwaukee, WI, USA
    Posts
    300
    First of all, what error messages are you getting, or if it compiles (Does it? Who knows? You didn't say.) what is going wrong with the program?

    On top of the extra parenthesis, your variable declaration/usage is off:
    You declare a, b, and salary as int. Do you really want them as integers? Probably not.
    You declare salary, but don't use it. That's just a waste. Do as aznboi said for that part of the program.

    Also, you tell the user to enter the hourly rate as $00.00. I'm sure you don't want them to put in the $. If not, don't tell them to do so. This also goes back to the fact that b is an integer. That's a no-no if you want the decimal part of b to have any meaning.

    Finally, the user is prompted to enter -1 if they want to quit. If I were to use this program and enter -1 for my hours worked, what would happen?
    [hint: the answer is not "the program would end."]

    OK, I said finally, but I was wrong. On an aesthetic note (and also, IIRC, part of 'C++' style programming) don't declare all of your variables at the beginning. Declare them right before you need them.

    Alright, alright, forget finally. We'll get to finally in a minute. Since we're talking aesthetics, make sure everything's tabbed (or spaced if you prefer) correctly and consistently. Also, for many statements, whitespace is ignored, so you can turn
    Code:
    cin>>this>>is>>a>>hard>>to>>read>>mess;
    into the much more readable
    Code:
    cin >> this >> is >> a >> nicer >> to >> read >> mess;
    Your code has no comments. Granted, most anyone can figure out what everything is doing here, but comments are a nice way to make sure.

    IIRC, though I don't use it yet, int main(int argc, char *argv[]) is the super-correct standard for a main function. This may be up to some debate, but I'm only writing it as another possible thing that "could be wrong with this program." (This is where you realize that I'm listing all of this as a way to let you know to be more specific. Asking "What's wrong with this code?" without any sort of qualification is like bringing your car to a mechanic and saying, "So, my car, right, it's making this noise. How much is that going to cost me?")

    Once these changes have been made, see if the program is working more to your liking. If not, try to figure out why not, post the new code, and let everyone know what it is that isn't right about your program. This will make it much easier to help you. (Not to mention that it will make everyone more willing to help you.)
    Last edited by Decrypt; 10-13-2005 at 11:56 PM.
    There is a difference between tedious and difficult.

  5. #5
    Railgun God |Wiz|'s Avatar
    Join Date
    Sep 2005
    Posts
    23
    And maybe you need to use "cin.ignore" after the "cin >> ... ;".
    And speaking of ignoring, ignore decrypt's
    Code:
    cout << this << is;
    and instead use
    Code:
    cout << "this" << "is";
    But I agree that with his aesthetics except for his "declare all the variables right before you use them."

    If you declare them at the beginning, the lag will be at the beginning and not in the middle, making for a much nicer program. How would you like it if you were about to blow up your worst enemy in Halo, and a group of variable declarations in the middle of the game lagged you so that you couldn't shoot?

    And it's better to use

    Code:
    cout << "this is a bunch of text";
    than

    Code:
    cout << "this" << "is" << "a" << "bunch" << "of" << "text";
    The only point in setting another set of "<<" is when you want to stop printing text and start printing variables or stop printing variables and start printing text.

    If you want to know what the difference between "a" and a is, then you REALLY need to brush up on your C++.

    And also, you need to use namespace std. A previous poster showed it but forgot to mention it.

    Poster as in "one who posts."
    Last edited by |Wiz|; 10-14-2005 at 05:57 PM.

  6. #6
    Registered User
    Join Date
    Jan 2005
    Posts
    7,344
    Decrypt's version is assuming those are variables. Your "corrected" version has the brackets the wrong way for cin.

    The cin.ignore() wouldn't hurt, but it is only useful if you have a call to get() or getline() after you call cin >>.

  7. #7
    Railgun God |Wiz|'s Avatar
    Join Date
    Sep 2005
    Posts
    23
    I accidently used "cin" and not "cout". I corrected it about a minute later.

    If you want to do cin, do what David said - use ">>".

    And cin.ignore DOES make a lot of difference - if you had string

    "x[250]"
    Then used
    "cin >> x"
    Then the "\0" would be stored inside of x.

    By the way, you would only be able to enter 249 characters inside of x.

    PS. It would be nice if the topic starter used a title like "Error thisisanerror appears in my program" instead of "Help!". Theonly reason I looked at this is because it was the oldest post I hadn't seen.
    Last edited by |Wiz|; 10-14-2005 at 05:59 PM.

  8. #8
    Moderately Rabid Decrypt's Avatar
    Join Date
    Feb 2005
    Location
    Milwaukee, WI, USA
    Posts
    300
    But I agree that with his aesthetics except for his "declare all the variables right before you use them."

    If you declare them at the beginning, the lag will be at the beginning and not in the middle, making for a much nicer program. How would you like it if you were about to blow up your worst enemy in Halo, and a group of variable declarations in the middle of the game lagged you so that you couldn't shoot?
    I added that since it's come up a few times in the last few months on this board. Every time I'd seen it come up, the consensus has been to avoid declaring at the top of a block of code. I searched the board and found these: (I didn't look at anything past the first page of threads, so there may be differing opinions from earlier threads.)

    Couple of questions
    Stops before the end of the code.

    I missed this one when it was current, but jverkoey disagreed here:
    Writing Code

    In any case, it seems its mostly a style issue, as long as the variables are declared as locally as possible. Lag in the executable isn't mentioned in any of the threads, which begs the question: How much lag does declaring and initializing a variable produce? I have no idea. Since the OP hasn't reposted or even replied yet, let's not hijack the thread, I'll bring it up in a new one.
    There is a difference between tedious and difficult.

  9. #9
    Registered User
    Join Date
    Jan 2005
    Posts
    7,344
    >> And cin.ignore DOES make a lot of difference - if you had string...

    I don't understand your example, and I don't know of any reason a cin.ignore() would help with code like that.

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