cin.get(); doesn't work even with cin.ignore();

This is a discussion on cin.get(); doesn't work even with cin.ignore(); within the C++ Programming forums, part of the General Programming Boards category; Looks good. A couple extra things to consider if you want. First, I prefer to start with a completely empty ...

  1. #16
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    Looks good.

    A couple extra things to consider if you want. First, I prefer to start with a completely empty file. I don't like that Dev-C++ starts you off with non-standard code, but I also think its better to just understand what you need and put it in when you need it. For example, you don't need <stdlib.h> in that program. You do need <iostream> for cout and cin, and you do need <string> for the string class. If you understand that, then for the next program you write you can decide which you need and only use those.

    A second thing is to work on your indenting (unless the forum software messed it up). Each time you add a new scope (basically in between your braces for the main function and the ifs and else ifs) you should indent the code four more spaces than the previous line. When the scope ends at the closing brace, go back to the previous indentation level. For example:
    Code:
        if ( age <= 12 && age > 0) {
            cout<<"You are a child!\n";
        }

  2. #17
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    Quote Originally Posted by calumn
    I used to have the && but someone on msn told me to change it to and and it still worked.
    I have changed the and back into && but it still gives me the exact same errors
    yeah, my mistake, I forgot that worked in C++ too... and don't listen to what people on msn tell you and don't read the msdn... evil lurks there.
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

  3. #18
    Registered User
    Join Date
    Nov 2005
    Posts
    545
    To change what Dev-C++ shows:

    1. Go to options
    2. Click on Environment Options
    3. Select the Misc. tab
    4. Change the code and press ok

  4. #19
    Registered User
    Join Date
    May 2006
    Posts
    34
    See they keep saying use && because its good programming practice and would sometimes bail half way out and you might get something wrong outputed.

  5. #20
    Registered User
    Join Date
    Mar 2006
    Posts
    725
    "and" is only there because some people in the remote mountains of Tahiti may not have "&" symbols on their typewriters. IMO you should refrain from using it whenever possible, as that is rather non-standard coding practice.
    Code:
    #include <stdio.h>
    
    void J(char*a){int f,i=0,c='1';for(;a[i]!='0';++i)if(i==81){
    puts(a);return;}for(;c<='9';++c){for(f=0;f<9;++f)if(a[i-i%27+i%9
    /3*3+f/3*9+f%3]==c||a[i%9+f*9]==c||a[i-i%9+f]==c)goto e;a[i]=c;J(a);a[i]
    ='0';e:;}}int main(int c,char**v){int t=0;if(c>1){for(;v[1][
    t];++t);if(t==81){J(v[1]);return 0;}}puts("sudoku [0-9]{81}");return 1;}

  6. #21
    Code Master calumn's Avatar
    Join Date
    May 2006
    Location
    Scotland
    Posts
    16
    Ok I wont use "and again.
    Do I need
    Code:
    using namespace std;
    at the start of every thing I make because It says to put it there in the tutorials but Dev C++ didn't have it there.
    I have added it anyway
    The program works fine with and without using namespace std; at the top. I kept it there anyway.
    This is my updated code and it tells you if you are an adult or child etc. even if you enter something like -22.
    Code:
    #include <iostream>
    #include <string>
    
    using namespace std;
    
    int main()
    {
     cout<<"Hello\n";
    cout<<"Please enter your first name and press enter\n";
    
    string name;
    
    cin>> name;
    cin.ignore();
    cout<<"Now enter your age\n";
    
    int age;
    
    cin>> age;
    cin.ignore();
    cout<<"So you are called "<< name <<" and you are "<< age <<" years old\n";
    if ( age <= 12 && age > 2) {
        cout<<"You are a child\n";
    }
    else if ( age >= 0 && age <= 2) {
        cout<<"You are a baby\n";
    }
    else if ( age >= 13 && age <= 19) {
        cout<<"You are a teenager\n";
    }
    else if ( age > 19 && age <= 55) {
        cout<<"You are an adult\n";
    }
    else if ( age >= 65 && age < 100) {
        cout<<"You are and OAP\n";
    }
    else if ( age < 0) {
        cout<<"You haven't been born yet\n";
    }
    else if (age >= 100) {
        cout<<"You are antique\n";
    }
    cin.get();
    return 0;
    }
    I also indented it before cout.
    I am going to try and make it so that it asks you if the details are right and if you say yes if syas something like OK but if you say no it goes back to asking you your name.

  7. #22
    Code Master calumn's Avatar
    Join Date
    May 2006
    Location
    Scotland
    Posts
    16
    Is it possible to use words inside and if statements
    Say I made a string called right and then it asked the user is the information was right or wrong and then there was an if statement that said if the variable was == to yes then it would say ok but if the variable was == no it would say Oh well then. Sfter I got that to work I would add loops so that it would go back to the start if you said no.
    Code:
    cout<<"Is this right?\n";
    string right;
    cin>> right;
    cin.ignore();
    
    if ( right == yes) {
        cout<<"OK\n";
    }
    else if ( right == no) {
         cout<<"Thats good then\n";
    }
    cin.get();
    I don't think it will work and I keep on getting errors while compiling:
    c:\docume~1\neilson\mydocu~1\progra~2\c__~1\abouty ~1.cpp: In function `int main()':
    c:\docume~1\neilson\mydocu~1\progra~2\c__~1\abouty ~1.cpp:48: `yes' undeclared (first use this function)
    c:\docume~1\neilson\mydocu~1\progra~2\c__~1\abouty ~1.cpp:48: (Each undeclared identifier is reported only once
    c:\docume~1\neilson\mydocu~1\progra~2\c__~1\abouty ~1.cpp:48: for each function it appears in.)
    c:\docume~1\neilson\mydocu~1\progra~2\c__~1\abouty ~1.cpp:51: `no' undeclared (first use this function)
    I think that it think that no and yes are a function.
    Have I just done it wrong and is there a better way of doing it.
    Thanks for all your help

  8. #23
    The Richness... Richie T's Avatar
    Join Date
    Jan 2006
    Location
    Ireland
    Posts
    469
    You can do this, but you must first understand that there are rules
    that the compiler uses to differentiate between variables and
    literal text. The compiler errors are telling you that the compiler
    thinks that yes and no are variables or a functions.

    To make it do the comparison, put those words in quotes, "yes"
    for example. Then your program will work because the compiler
    knows it is text, refered to as a string literal.

    Just an early warning though, this works fine for C++ strings,
    but you will (sooner or later) come across C style strings - less
    fancy and powerful. To do comparisons on those you need the
    function strcmp (). No need to worry about it yet though.
    No No's:
    fflush (stdin); gets (); void main ();


    Goodies:
    Example of fgets (); The FAQ, C/C++ Reference


    My Gear:
    OS - Windows XP
    IDE - MS Visual C++ 2008 Express Edition


    ASCII stupid question, get a stupid ANSI

  9. #24
    Code Master calumn's Avatar
    Join Date
    May 2006
    Location
    Scotland
    Posts
    16
    Thanks
    I have changed it to that and it works fine.
    I have made it also ask you if it is right and it can tell if you are not telling the truth about your age.
    This is the updated code
    What do you think I should try to add next
    Code:
    #include <iostream>
    #include <stdlib.h>
    #include <string>
    #include <stdio.h>
    
    using namespace std;
    
    int main()
    {
    cout<<"Hello\n";
    cout<<"Please enter your first name and press enter\n";
    
    string name;
    
    cin>> name;
    //Clear screen:
    
    system("cls");
    ageenter: /*Makes ageenter*/
    cout<<"Now enter your age\n";
    
    int age;
    
    cin>> age;
    system("cls"); /*Clears the screen*/
    if (age < 1 ) {
        cout << name << ", you are not " << age << "\n";
    goto ageenter; /*goes to ageenter*/
    }
    else if (age > 124 ) {
        cout << name << ", you are not " << age << "\n";
    goto ageenter;
    }
    else
    {
    cout<<"So you are called "<< name <<" and you are "<< age <<" years old\n";
    
    if ( age < 12 && age > 0 ) {
        cout<<"You are a child!\n";
    }
    else if ( age >= 13 && age <= 19) {
        cout<<"You are a teenager\n";
    }
    else if ( age > 19 && age <= 55) {
        cout<<"You are an adult\n";
    }
    else
    {
        cout<<"You are elderly";
    }
    }
    
    cout<<"Is this right?\n";
    string right;
    cin>> right;
    if (right == "yes") {
        cout<<"OK\n";
        system("pause");
    }
    else if (right == "no") {
    goto ageenter;
    }
    
    return 0;
    }

  10. #25
    Code Master calumn's Avatar
    Join Date
    May 2006
    Location
    Scotland
    Posts
    16
    I am trying to make it open in full screen.
    I don't know how to do this but I asked one of my friends said you have to make it so that the program thinks that the user is pressing alt-enter by doing this:
    Add the windows.h library and then add:
    Code:
    keybd_event(VK_MENU, MapVirtualKey(VK_MENU, 0), 0, 0); 
        keybd_event(VK_RETURN, MapVirtualKey(VK_RETURN, 0), 0, 0); 
        keybd_event(VK_RETURN, MapVirtualKey(VK_RETURN, 0), KEYEVENTF_KEYUP, 0); 
        keybd_event(VK_MENU, MapVirtualKey(VK_MENU, 0), KEYEVENTF_KEYUP, 0);
    Is it hard to make them so that they don't run in DOS/command prompt and run in a windows sort of thing if you know what I mean or is that way too hard.

  11. #26
    The Richness... Richie T's Avatar
    Join Date
    Jan 2006
    Location
    Ireland
    Posts
    469
    Program works well, just a few tips though

    1) The use of goto is "frowned upon" (thats the wording i see a
    lot), basically due to the visual and logical effect that it has on
    your code - programmers don't like to go looking for labels, and
    repeated use leads to code that is difficult to follow and can cause
    debugging issues (not from personal experience, I've never used
    goto). Nevertheless, it seems to be very natural to beginners,
    so that i suggest that the next couple of programs you write,
    use goto and get a feel for controlling the flow of your program.
    Then learn about loops - the more clearer and definite way to
    achieve program flow control. Here's a link about them.

    2) You don't need stdio.h for this program - thats a C header
    anyway. Also, as for stdlib.h (also C), the C++ equivalent is
    cstdlib (no .h). Either will work in this program, but just so you
    know.

    3) As for opening a console in full screen, yes Virtual Keyboard
    (VK) is one way of doing it for you, but there is probably an
    actual function in the windows header that allows you to do
    this. I don't know any off the top of my head because I haven't
    done any "windows programming," but perhaps someone else
    can suggest a method. Nevertheless, I'd like to make the point
    that since you are a beginner, messing with VK and windows
    programming features is ill advised, because you are learning
    code that is very system specific. You can do loads of things with
    VK but that doesn't mean there isn't a simpler and better way to
    do it with standard code. My advice is to hold back on
    implementing such a feature.

    4) Making it look like a windows dialog box is much much harder,
    and with what i said in 3 above, you really should wait a good
    while before you mingle with windows API programming.

    5) Code indentation has been mentioned earlier on, but I thought
    I'd reiterate here.

    The next steps your program could make would be to allow the
    user to enter yes or no in any case (i.e. YeS). Take a look at
    a function called tolower or toupper, or compare to every
    permutation of the letters (inefficient but it can be done). I won't
    give you a specific way of doing this, so you can do some
    investigations and find out for yourself. Nonetheless, if you
    decide to try it and run into problems, just call back here and you
    can get help.

    Also I'm guessing that you haven't learned about function yet
    (such as writing your own, passing data to and from them etc.)
    Take a look at this as well.
    No No's:
    fflush (stdin); gets (); void main ();


    Goodies:
    Example of fgets (); The FAQ, C/C++ Reference


    My Gear:
    OS - Windows XP
    IDE - MS Visual C++ 2008 Express Edition


    ASCII stupid question, get a stupid ANSI

  12. #27
    Code Master calumn's Avatar
    Join Date
    May 2006
    Location
    Scotland
    Posts
    16
    I think that I have over complicted things with my loops.
    I am getting 3 errors right now.
    Code:
    #include <iostream>
    #include <stdlib.h>
    #include <string>
    #include <stdio.h>
    
    using namespace std;
    
    int main()
    {
    cout<<"Hello\n";
    cout<<"Please enter your first name and press enter\n";
    
    string name;
    
    cin>> name;
    //Clear screen:
    
    system("cls");
    
    cout<<"Now enter your age\n";
    
    int age;
    
    cin>> age;
    system("cls"); /*Clears the screen*/
    if (age < 1 ) {
        cout << name << ", you are not " << age << "\n";
    
    }
    else if (age > 124 ) {
        cout << name << ", you are not " << age << "\n";
    
    }
    else
    {
    cout<<"So you are called "<< name <<" and you are "<< age <<" years old\n";
    
    if ( age < 12 && age > 0 ) {
        cout<<"You are a child!\n";
    }
    else if ( age >= 13 && age <= 19) {
        cout<<"You are a teenager\n";
    }
    else if ( age > 19 && age <= 55) {
        cout<<"You are an adult\n";
    }
    else
    {
        cout<<"You are elderly\n";
    }
    }
    
    cout<<"Is this right?\n";
    string right;
    cin>> right;
    
    if (right == "no") {
    while ( right == "no" ) {
        cout<<"Enter your age\n";
        cin>> age;
        system("cls"); /*Clears the screen*/
    if (age < 1 ) {
        cout << name << ", you are not " << age << "\n";
    
    }
    else if (age > 124 ) {
        cout << name << ", you are not " << age << "\n";
    
    }
    else
    {
        cout<<"So you are called "<< name <<" and you are "<< age <<" years old\n";
    
    if ( age < 12 && age > 0 ) {
        cout<<"You are a child!\n";
    }
    else if ( age >= 13 && age <= 19) {
        cout<<"You are a teenager\n";
    }
    else if ( age > 19 && age <= 55) {
        cout<<"You are an adult\n";
    }
    else
    {
        cout<<"You are elderly\n";
    }
    }
    }
    else if ( right == "yes") {
        cout<<"Ok, Press enter or Esc to exit\n";
        cin.get()
    }
    }
    return 0;
    }
    I know VB 6 and .NET
    I also know actionscripting in Flash and I am OK in python if that counts and was learning pascal but gave up because I could find a good compiler. I am now learning C++ and I think its reall cool.

  13. #28
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,529
    Code:
    if (right == "no") { /* Not necessary */
    while ( right == "no" ) {
    Yep, you've got a lot of syntax errors, I can tell just by running it through a syntax checker. However, I don't think it's the fault of adding a loop. You really need to adopt a cleaner coding style that is easier to read.
    Code:
    int main (void)
    {
        if (condition) 
        {
            do stuff;
            do this too;
            and this;
        }
        else 
        {
            try this instead;
        }
        return 0;
    }
    In my psuedo-code you can tell by eyeballing where one code block ends and another begins.
    Last edited by whiteflags; 05-13-2006 at 08:44 AM.

  14. #29
    The Richness... Richie T's Avatar
    Join Date
    Jan 2006
    Location
    Ireland
    Posts
    469
    You had a lot of code duplication there, no need. Here is your
    code tweaked around a little and reformatted to be easier to
    read:

    Code:
    #include <iostream>
    #include <string>
    #include <cstdlib>
    
    using namespace std;
    
    int main(){
    
    	string right = "no";
    
    	cout<<"Hello\n";
    
    	while (right == "no"){
    		cout<<"Please enter your first name and press enter\n";
    
    		string name;
    		cin>> name;
    		system("cls");
    
    		cout<<"Now enter your age\n";
    		int age;
    		cin>> age;
    
    		system("cls");
    
    		if (age < 1 ) {
    			cout << name << ", you are not " << age << "\n";
    		}
    
    		else if (age > 124 ) {
    			cout << name << ", you are not " << age << "\n";
    		}
    
    		else{
    			cout<<"So you are called "<< name <<" and you are "<< age <<" years old\n";
    
    			if ( age < 12 && age > 0 ) {
    				cout<<"You are a child!\n";
    			}
    
    			else if ( age >= 13 && age <= 19) {
    				cout<<"You are a teenager\n";
    			}
    
    			else if ( age > 19 && age <= 55) {
    				cout<<"You are an adult\n";
    			}
    
    			else{
    				cout<<"You are elderly\n";
    			}
    		}
    
    		cout<<"Is this right?\n";
    		cin>> right;
    
    		while ((right != "yes") && (right != "no")){
    
    			if ((right != "yes") && (right != "no")){
    				cout<<"Please enter yes or no\n";
    				cout<<"Is this right?\n";
    				cin>> right;
    			}
    		}
    	}
    
        cout<<"Ok, Press enter or Esc to exit\n";
        cin.get();
    
    	return 0;
    }
    This should work for you, and I added a check to see whether
    yes or no was not entered. The code should be fairly
    straightforward to you now.
    No No's:
    fflush (stdin); gets (); void main ();


    Goodies:
    Example of fgets (); The FAQ, C/C++ Reference


    My Gear:
    OS - Windows XP
    IDE - MS Visual C++ 2008 Express Edition


    ASCII stupid question, get a stupid ANSI

  15. #30
    Code Master calumn's Avatar
    Join Date
    May 2006
    Location
    Scotland
    Posts
    16
    Thanks for helping me again.
    I will try to write the code in a neater style and in all languages everyone knows that i am very messy. Thanks for the help. I see what you have done because at first I was wondering how it knew that the string called right was no but then I saw it being made at the very start.
    I know VB 6 and .NET
    I also know actionscripting in Flash and I am OK in python if that counts and was learning pascal but gave up because I could find a good compiler. I am now learning C++ and I think its reall cool.

Page 2 of 3 FirstFirst 123 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problems in getting OpenGL to work
    By zonf in forum C Programming
    Replies: 5
    Last Post: 02-13-2006, 03:48 AM
  2. Why won't my OpenGL work?
    By Raigne in forum C++ Programming
    Replies: 7
    Last Post: 11-26-2005, 10:53 AM
  3. help getting program to work
    By jlmac2001 in forum C Programming
    Replies: 2
    Last Post: 11-13-2002, 10:04 PM
  4. fopen();
    By GanglyLamb in forum C Programming
    Replies: 8
    Last Post: 11-03-2002, 11:39 AM
  5. DLL __cdecl doesnt seem to work?
    By Xei in forum C++ Programming
    Replies: 6
    Last Post: 08-21-2002, 04:36 PM

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