CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 13 of 13

Thread: Help a newbi

  1. #1
    Join Date
    Apr 2010
    Posts
    60

    Help a newbi

    Ok so i'm trying to write something very simple, I'm a complete newbi.

    #include <iostream>

    using namespace std;

    int age;
    char answer;

    int main()
    {
    cout << "Hello, And welcome to my Quiz, This is just a basic Program\n\n";
    system("PAUSE");
    system("CLS");

    cout << "Please enter your Age\n";
    cin >> age;
    cout << "So you are " << age << " Years old?, Y/N \n";
    cin >> answer;
    if(answer == "Y" || answer == "y")
    {
    cout << "Ok ";
    }

    system("PAUSE");
    }

    If you press N i wan't it to go Back to where you enter your age, How do i send it back to that line?, Or do i just ask the question again?
    Last edited by UpcomingChris; November 20th, 2011 at 06:09 PM.

  2. #2
    Join Date
    Oct 2006
    Location
    Sweden
    Posts
    3,654

    Re: Help a newbi

    You can use for instance a do-while loop.
    An observation: answer == "Y" compares if answer is the same as the address of the literal "Y". You should use 'Y' to compare with a character.

    Have you tried to compile this yet? I bet the compiler has some things to tell you...

    Edit: If you wrap your code inside code tags the indentation is preserved. [code]Your code here[/code]
    Last edited by S_M_A; November 20th, 2011 at 06:17 PM.
    Debugging is twice as hard as writing the code in the first place.
    Therefore, if you write the code as cleverly as possible, you are, by
    definition, not smart enough to debug it.
    - Brian W. Kernighan

    To enhance your chance's of getting an answer be sure to read
    http://www.codeguru.com/forum/announ...nouncementid=6
    and http://www.codeguru.com/forum/showthread.php?t=366302 before posting

    Refresh your memory on formatting tags here
    http://www.codeguru.com/forum/misc.php?do=bbcode

    Get your free MS compiler here
    https://visualstudio.microsoft.com/vs

  3. #3
    Join Date
    Apr 2010
    Posts
    60

    Re: Help a newbi

    Quote Originally Posted by S_M_A View Post
    You can use for instance a do-while loop.
    An observation, answer == "Y" compares if answer is the same as the address of the literal "Y". You should use 'Y' to compare with a character.

    Have you tried to compile this yet? I bet the compiler has some things to tell you...
    indeed it did, i edited it a little bit and it's all working, just don't know how to do the loop, Can you explain it to me?



    #include <iostream>
    #include <string>

    using namespace std;

    int age;
    string answer;

    int main()
    {
    cout << "Hello, And welcome to my Quiz, This is just a basic Program\n\n";
    system("PAUSE");
    system("CLS");

    cout << "Please enter your Age\n";
    cin >> age;
    cout << "So you are " << age << " Years old?, Y/N \n";
    cin >> answer;
    if(answer == "Y" || answer == "y")
    {
    cout << "Ok ";
    }
    else
    {
    cout << "Ok, Please enter your correct age.\n";
    }
    system("PAUSE");
    }

  4. #4
    Join Date
    Jun 2010
    Location
    Germany
    Posts
    2,675

    Re: Help a newbi

    Your first concern rather should be whether what you currently have actually compiles (and works), and this certainly does not:

    Quote Originally Posted by UpcomingChris View Post
    Code:
        if(answer == "Y" || answer == "y")
    You can't extract data from cin into string literals, the first and probably most obvious reason for this being that these literals are read-only. Instead you'd need to read the data from cin into some variable and then compare that to your literals.

    I do get what you want to express with this line, but the compiler is comparatively dumb and stubborn and insists on you using proper C++ syntax and semantics. The compiler entirely lacks any sort of fantasy or imagination.

    If you press N i wan't it to go Back to where you enter your age, How do i send it back to that line?, [...]
    Obviously, you need a loop for that.

    [...] Or do i just ask the question again?
    I wouldn't recommend that approach. Imagine you need to accomodate to a stubborn user who enters the wrong age, say, 20 or so times (and each time admits the mistake). That would obviously be pretty tideous for you as the programmer. And then, when you're done with the program, comes an even denser user who does that 21 times...

    And please use code tags when posting code.

    EDIT: Note: This post is based on the version of the OP prior to the edit from 12:09 AM CET.
    Last edited by Eri523; November 20th, 2011 at 06:23 PM.
    I was thrown out of college for cheating on the metaphysics exam; I looked into the soul of the boy sitting next to me.

    This is a snakeskin jacket! And for me it's a symbol of my individuality, and my belief... in personal freedom.

  5. #5
    Join Date
    Jun 2010
    Location
    Germany
    Posts
    2,675

    Re: Help a newbi

    Quote Originally Posted by UpcomingChris View Post
    [...] just don't know how to do the loop, Can you explain it to me?
    http://www.cplusplus.com/doc/tutorial/control/
    I was thrown out of college for cheating on the metaphysics exam; I looked into the soul of the boy sitting next to me.

    This is a snakeskin jacket! And for me it's a symbol of my individuality, and my belief... in personal freedom.

  6. #6
    Join Date
    Apr 2010
    Posts
    60

    Re: Help a newbi

    I'm not extracting a cin straight into literals, I'm extracting the cin into a String, and then extracting the String into 'literals' aren't i?, or am i looking at it wrong?

  7. #7
    Join Date
    Apr 2010
    Posts
    60

    Re: Help a newbi

    Thanks for the link, now to get it to work with my code!, Going to get the hang of this sometime soon...

  8. #8
    Join Date
    Oct 2006
    Location
    Sweden
    Posts
    3,654

    Re: Help a newbi

    Please don't edit your posts after you get an answer (at least not vital parts).
    Eri523 gave you a good link regarding the do-while
    Debugging is twice as hard as writing the code in the first place.
    Therefore, if you write the code as cleverly as possible, you are, by
    definition, not smart enough to debug it.
    - Brian W. Kernighan

    To enhance your chance's of getting an answer be sure to read
    http://www.codeguru.com/forum/announ...nouncementid=6
    and http://www.codeguru.com/forum/showthread.php?t=366302 before posting

    Refresh your memory on formatting tags here
    http://www.codeguru.com/forum/misc.php?do=bbcode

    Get your free MS compiler here
    https://visualstudio.microsoft.com/vs

  9. #9
    Join Date
    Jun 2010
    Location
    Germany
    Posts
    2,675

    Re: Help a newbi

    Quote Originally Posted by UpcomingChris View Post
    I'm not extracting a cin straight into literals, [...] or am i looking at it wrong?
    It's relatively late at night right now here, but I'm pretty sure you did prior to editing your post. However, I just noticed that even my quote from your post in post #4 already is the fixed version. So I don't have any proof...
    I was thrown out of college for cheating on the metaphysics exam; I looked into the soul of the boy sitting next to me.

    This is a snakeskin jacket! And for me it's a symbol of my individuality, and my belief... in personal freedom.

  10. #10
    Join Date
    Apr 2010
    Posts
    60

    Re: Help a newbi

    Quote Originally Posted by S_M_A View Post
    Please don't edit your posts after you get an answer (at least not vital parts).
    Eri523 gave you a good link regarding the do-while
    i didn't, I edited my post before any comment, to my knowledge, was left. And i thank'd him for the link. And i have a new problem!.

    Code:
     #include <iostream>
    #include <string>
    
    using namespace std;
    
    int age;
    string answer;
    
    int main()
    {
        cout << "Hello, And welcome to my Quiz, This is just a basic Program\n\n";
             system("PAUSE");   
             system("CLS");
        
        cout << "Please enter your Age\n";
        cin >> age;
        cout << "So you are " << age << " Years old?, Y/N \n";
        cin >> answer;
        
        
        while(answer == "N" || answer == "n")
           {
               cout << "Please enter your correct age ";
           }              
        
          cout << "Ok\n";  
        
             system("PAUSE");
    }
    It just spams "Please enter your correct age", I guess it's becasue once you assign n to 'answer' it won't change unless you change it again, which i don't know how to do yet......?

  11. #11
    Join Date
    Oct 2006
    Location
    Sweden
    Posts
    3,654

    Re: Help a newbi

    This is the visual studio forum, have you tried running this in the debugger? I'm not sure what I respond to anymore... you know you can preview your post i.e. completing it before actually posting it?

    Yes, you have to re-assign answer to break the loop. I would have done it something like this (the break variant just because you had two different outputs before reading the age)
    Code:
    cout << "Please enter your Age\n";
    while(1)
    {
        cin >> age;
        cout << "So you are " << age << " Years old?, Y/N \n";
        cin >> answer;
        if( answer == "Y" || answer == "y" ) break;
        cout << "Please enter your correct age ";
    }
    Last edited by S_M_A; November 20th, 2011 at 06:48 PM.
    Debugging is twice as hard as writing the code in the first place.
    Therefore, if you write the code as cleverly as possible, you are, by
    definition, not smart enough to debug it.
    - Brian W. Kernighan

    To enhance your chance's of getting an answer be sure to read
    http://www.codeguru.com/forum/announ...nouncementid=6
    and http://www.codeguru.com/forum/showthread.php?t=366302 before posting

    Refresh your memory on formatting tags here
    http://www.codeguru.com/forum/misc.php?do=bbcode

    Get your free MS compiler here
    https://visualstudio.microsoft.com/vs

  12. #12
    Join Date
    Apr 2010
    Posts
    60

    Re: Help a newbi

    Apologies, i seem to be in the wrong part, i'm using DevC++, But can you suply the solution to leave the loop?

  13. #13
    Join Date
    Oct 2006
    Location
    Sweden
    Posts
    3,654

    Re: Help a newbi

    If remember correctly DevC++ is not maintained anymore?
    Looking at their homepage they say it's using gcc 3.4.2 and that was released September 6, 2004! Get yourself a more modern compiler like MS Visual Studio Express for instance.
    Debugging is twice as hard as writing the code in the first place.
    Therefore, if you write the code as cleverly as possible, you are, by
    definition, not smart enough to debug it.
    - Brian W. Kernighan

    To enhance your chance's of getting an answer be sure to read
    http://www.codeguru.com/forum/announ...nouncementid=6
    and http://www.codeguru.com/forum/showthread.php?t=366302 before posting

    Refresh your memory on formatting tags here
    http://www.codeguru.com/forum/misc.php?do=bbcode

    Get your free MS compiler here
    https://visualstudio.microsoft.com/vs

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured