code help?

This is a discussion on code help? within the C++ Programming forums, part of the General Programming Boards category; Code: #include <cstdlib> #include <iostream> using namespace std; int main(int argc, char *argv[]) { int age cout<<"What's your age?" cin>>age; ...

  1. #1
    Registered User
    Join Date
    Dec 2004
    Posts
    465

    code help?

    Code:
    #include <cstdlib>
    #include <iostream>
    
    using namespace std;
    
    int main(int argc, char *argv[])
    
    {
        int age
        
        cout<<"What's your age?"
        cin>>age;
        if (age>0){
        cout<<"Get shelby runescape!"
    }
        system("PAUSE");
        return EXIT_SUCCESS;
    }
    When I compile it says sytax error before << after the first cout.
    My computer is awesome.

  2. #2
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    Missing semi-colon. Count your }s.
    *Several missing semi-colons (.
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

  3. #3
    Registered User
    Join Date
    Dec 2004
    Posts
    465
    heh heh oops I'm new to this how do I make not say press any key to continue?
    Last edited by cerin; 01-11-2005 at 09:49 PM.
    My computer is awesome.

  4. #4
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    Take out the 'system("PAUSE");' line. The screnn will not stay up long, but the FAQ will help you fix that.
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

  5. #5
    Registered User
    Join Date
    Dec 2004
    Posts
    465
    I'm sorry but I don't see that in the faq.
    My computer is awesome.

  6. #6
    Registered User
    Join Date
    Dec 2004
    Posts
    465
    Code:
    #include <cstdlib>
    #include <iostream>
    
    using namespace std;
    
    int main(int argc, char *argv[])
    
    {
        int age;
        char yes;
        
        cout<<"What's your age?";
        cin>>age;
        if (age>0);{
        cout<<"Get shelby runescape!";
    }
    {
        cout<<"how about it? y/n?";
    }
        cin>>yes;
        switch(yes){
        case'n':
        cout<<"gggggggggrrrrrrrrrrrrrrr";
        break;
        
        default:
        cout<<"not a valid choice";
    }
        system("PAUSE");
        return EXIT_SUCCESS;
    }
    This is the first program I've written by myself. I showed it to my mom in hopes of her getting me runescape(mmorpg). I guess this is the finished product.
    My computer is awesome.

  7. #7
    Registered User Kybo_Ren's Avatar
    Join Date
    Sep 2004
    Posts
    136
    The best way to pause execution so you can see output is first to flush the input buffer
    Code:
    std::cin.ignore(std::numeric_limits<streamsize>::max(),'\n');
    then wait for new input
    Code:
    std::cin.get()
    EDIT: Please indent the braces. It is much easier to see the program flow if you indent the braces.
    Uhhh, does that program compile? The braces are misplaced, and you're not giving a 'y' option in the switch, so if your mom says, 'yeah, I'll get him Runescape', she'll get "invalid option' Not what you want, most likely.
    EDIT2: Oh, and if you're going to use this, make sure you #include <limits>
    Last edited by Kybo_Ren; 01-11-2005 at 10:49 PM.

  8. #8
    Registered User
    Join Date
    Dec 2004
    Posts
    465
    Quote Originally Posted by Kybo_Ren
    Code:
    std::cin.ignore(std::numeric_limits<streamsize>::max(),'\n');
    How do you write this w/o std:: and where does it go?
    Code:
    std::cin.get()
    Is this in place of system pause?
    Uhhh, does that program compile? The braces are misplaced, and you're not giving a 'y' option in the switch, so if your mom says, 'yeah, I'll get him Runescape', she'll get "invalid option' Not what you want, most likely.
    Ya the program compiles. Which braces are misplaced? I don't give a y option because I pretty much knew the answer. I bothered her for about 2 weeks before I got Runescape the first time.
    My computer is awesome.

  9. #9
    Registered User Kybo_Ren's Avatar
    Join Date
    Sep 2004
    Posts
    136
    Code:
    cout<<"Get shelby runescape!";
    }
    {
        cout<<"how about it? y/n?";
    }
        cin>>yes;
        switch(yes){
        case'n':
        cout<<"gggggggggrrrrrrrrrrrrrrr";
        break;
        
        default:
        cout<<"not a valid choice";
    }
    That looks pretty weird to me. Maybe it's just me, though.

    And std:: is just the namespace qualifier. You don't need it if you specified using namespace std; or using std::numeric_limits; and using std::cin;

    Those two lines should go together (std::cin.ignore() first) where you want to pause.

  10. #10
    Registered User
    Join Date
    Dec 2004
    Posts
    465
    Could you tell me which brackets are misplaced or is it to much trouble? If so I'm not too worried about them.
    My computer is awesome.

  11. #11
    Sweet
    Join Date
    Aug 2002
    Location
    Tucson, Arizona
    Posts
    1,807
    By misplaced he means not idented this is how you would want the to look with your coding style(which I am guessing)
    Code:
    int main(void)
    {
    
      if(age > 0){ //use spaces in your if's it is nicer to read that way
        
        cout<<"Whatever you want to print"<<endl;
        
      }//notice how this isn't lined up to the right side
    
    }
    Woop?

  12. #12
    Registered User Kybo_Ren's Avatar
    Join Date
    Sep 2004
    Posts
    136
    Well, I'd have written this code like:
    Code:
    #include <cstdlib>
    #include <iostream>
    #include <limits>
    
    using namespace std;
    
    int main(int argc, char *argv[])
    {
        int age;
        char yes;
        
        cout<<"What's your age?" << std::flush;
        cin>>age;
        if (age>0)
        {
            cout<<"Get shelby runescape!" << std::flush;
        }
    
        cout<<"how about it? y/n?" << std::flush;
    
        cin>>yes;
        switch(yes)
        {
        case'n':
            cout<<"gggggggggrrrrrrrrrrrrrrr" << std::flush;
            break;
        
        default:
            cout<<"not a valid choice" << std::flush;
            break;
        }
    
        std::cin.ignore(std::numeric_limits<streamsize>::max(),'\n');
        std::cin.get();
        return EXIT_SUCCESS;
    }
    They actually weren't misplaced, it was the indentation that was messingme up.
    You can do braces on the same line as the statement, but it's purely personal preference.

    I removed the braces around that cout statement because they weren't needed (unless you wanted it to cout that information if age was <= 0).

    I also flushed the output buffer after each cout... it's pretty much habit. I'm sure the FAQ on this site has something about flushing the output buffers.

    I also added a break; to the end of the default case. It doesn't do anything in that example, but if you ever add statements after that, it will not do what you want, and can make debugging hell. I did it once and learned my lesson.

    And, of course, I also changed the system() call (the FAQ definitely has something about the paradox of the system() call) to my preferred method.

    Also, I always add the std:: qualifier before anything I use from the standard library. It's unneccessary in this case because you use 'using namespace std;', but I always qualify my identifiers because:
    1.) It's immediately apparent that you're using something from the standard library when you use the std namespace qualifier
    2.) I don't use 'using namespace std;' because it effectively defeats the point of using namespaces, so the std qualifier becomes neccessary (unless I use a 'using std::xxxxx', but I really tend not to)

  13. #13
    Registered User
    Join Date
    Dec 2004
    Posts
    465
    Alright thanks a bunch. I'll try to make it neater as you guys suggested.

    Edit:Should I put end1 after each cout?
    Last edited by cerin; 01-11-2005 at 11:16 PM.
    My computer is awesome.

  14. #14
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,802
    Quote Originally Posted by cerin
    Edit:Should I put end1 after each cout?
    It is endl, not end1, that is an 'l' (as in 'L') and not a '1' (as in 'one'). endl means end-line.

    It is not absolutely needed for purposes of flushing the output stream (there may be odd cases where it is necessary), the i/o streams cin and cout are suppossed to be tied to each other so that any cin "should" automatically flush the tied cout stream, but it won't hurt to do that. I.e by default the following is supposed to happen automatically:

    Code:
    std::cin.tie(&std::cout);
    This ensures that when you use cout to prompt the user to enter input using cin that cout gets flushed.

    Code:
    int age;
    cout << "Enter your age: ";
    cin >> age; // cout should be flushed automatically because they are tied together
    Last edited by hk_mp5kpdw; 01-12-2005 at 05:41 AM.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Extended ASCII Characters in an RTF Control
    By JustMax in forum C Programming
    Replies: 18
    Last Post: 04-03-2009, 08:20 PM
  2. Enforcing Machine Code Restrictions?
    By SMurf in forum Tech Board
    Replies: 21
    Last Post: 03-30-2009, 07:34 AM
  3. Obfuscated Code Contest
    By Stack Overflow in forum Contests Board
    Replies: 51
    Last Post: 01-21-2005, 03:17 PM
  4. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 05:06 PM
  5. Replies: 0
    Last Post: 02-21-2002, 05:05 PM

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