im new to c++ and i really need help

This is a discussion on im new to c++ and i really need help within the C++ Programming forums, part of the General Programming Boards category; well as i said im realy realy new and i just wondered if there was a way to make a ...

  1. #1
    Registered User
    Join Date
    Jun 2006
    Posts
    7

    im new to c++ and i really need help

    well as i said im realy realy new and i just wondered if there was a way to make a if command saying
    if input char is y run if input char is n close so could someone plz help me

  2. #2
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,801
    You need to know the basics of I/O using cin/cout.

    1) Declare a character variable.
    2) Prompt user for input using cout.
    3) Input into the above character variable using cin.
    4) Test the character entered by user via an if statement and perform the appropriate action based on result of the test.
    "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

  3. #3
    The Richness... Richie T's Avatar
    Join Date
    Jan 2006
    Location
    Ireland
    Posts
    469
    Code:
    char a_char;
    
    //program does some stuff - read input etc.
    
    if (a_char == 'y')
    {
        //do stuff
    }
    
    else if (a_char == 'n')
    {
        //do stuff
    }
    next time read a tutorial
    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

  4. #4
    Registered User
    Join Date
    Jun 2006
    Posts
    7
    thanks its helped me a lot and thanks richie t for the link to those tutorials i hadnt seen them
    neway thanks again for the help

  5. #5
    Registered User
    Join Date
    Jun 2006
    Posts
    7
    do you no what is rong with this code because i cant work it out
    Code:
    #include <iostream>
    
    using namespace std;
    
    int main()
    {
    char a_char;
    
    char answer;
    cout<<"Would you like to run this program y = yes n = no: ";
    cin>> answer; 
    if (a_char == 'y')
    {
      int age;                            
      
      cout<<"Please input your age: ";    
      cin>> age;                          
      cin.ignore();                       
      if ( age < 100 ) {                  
         cout<<"You are pretty young!\n"; 
      }
      else if ( age == 100 ) {            
         cout<<"You are old\n";           
      }
      else {
        cout<<"You are really old\n";     
      }
      cin.get();
    }
    
    else if (a_char == 'n')
    {
     return EXIT_SUCCESS; 
    }

  6. #6
    Registered User
    Join Date
    Jan 2005
    Posts
    7,318
    Code:
    cin>> answer; 
    if (a_char == 'y')
    Notice anything wrong with that code, in particular the variables you are using?

  7. #7
    Registered User
    Join Date
    Jan 2005
    Posts
    183
    Your storing the yes / no ( y / n ) in the variable: answer, but your checking if the variable: a_char equals y / n. If I'm not mistaken, a_char will always equal nothing in your program. And also, add another } at the end, as your'e not closing main().

    I think this is what you want:

    Code:
    #include <iostream>
    
    using namespace std;
    
    int main()
    {
        char a_char;
    
        cout<<"Would you like to run this program y = yes n = no: ";
        cin>> a_char; 
        
        if (a_char == 'y')
        {
             int age;                            
      
             cout<<"Please input your age: ";    
             cin>> age;                          
             cin.ignore();                       
             
             if ( age < 100 ) 
             {                  
                 cout<<"You are pretty young!\n"; 
             }
      
             else if ( age == 100 )
             {            
                 cout<<"You are old\n";           
             }
      
             else 
             {
                  cout<<"You are really old\n";     
             }
      
             cin.get();
        }
    
        else if (a_char == 'n')
        {
             return EXIT_SUCCESS; 
        }
    
    }
    It's always a good idea to indent your code, as it makes it much easier to read. Also, it would be quite helpful to handle any unexpected input: e.g. If the user enteres a letter when asked for his/her age, or if they don't enter either y or n at the start. If you have any questions, please do not hesitate to contact me at: School.

    Cya.

    P.S. I've probably made some sort of fatal mistake in the code above, so if any of the other members have the time, please alert me of any errors so I can edit them for him. Thanks
    Last edited by Necrofear; 06-30-2006 at 09:14 AM.

  8. #8
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Portugal
    Posts
    7,467
    What happens if a_char is neither 'y' or 'n'? Also, answer is being declared but not used. What happens if the user inserts an invalid age?
    The programmer’s wife tells him: “Run to the store and pick up a loaf of bread. If they have eggs, get a dozen.”
    The programmer comes home with 12 loaves of bread.


    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  9. #9
    Registered User
    Join Date
    Jun 2006
    Posts
    7
    hi sorry to ask realy perthetic questions but how do i tell it to go back to a set place in the code instead of just starting the if loop again

  10. #10
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Portugal
    Posts
    7,467
    The tutorials explain about loops, conditions, and functions, shadowless. The main elements in controling program flow.
    The programmer’s wife tells him: “Run to the store and pick up a loaf of bread. If they have eggs, get a dozen.”
    The programmer comes home with 12 loaves of bread.


    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  11. #11
    Registered User
    Join Date
    Jun 2006
    Posts
    7
    ok so ive checked my cod a bit and yet still it doesnt seem to be quite right
    Code:
    #include <iostream>
    
    using namespace std;
    
    int main()
    {
        char a_char;
    
        cout<<"Would you like to run this program y = yes n = no \n";
        cin>> a_char; 
        
        if (a_char == 'y')
        {
             int age;                            
      
             cout<<"Please input your age: ";    
             cin>> age;                          
             cin.ignore();                       
             
             if ( age < 100 ) 
             {                  
                 cout<<"You are pretty young!\n"; 
             }
      
             else if ( age == 100 )
             {            
                 cout<<"You are old\n";           
             }
      
             else if ( age > 100 )
             {
                  cout<<"You are really old\n";     
             }
             
             else
             {
                 cout<<"that is an invalid charector, application will now close\n";    // this is the                           bit that doesnt work no matter what i do it just doesnt run that section
             }
             cin.get();
             
        }
    
        else if (a_char == 'n')
        {
             return EXIT_SUCCESS; 
        }
        else    
        {
                 cout<<"that is an invalid charector, application will now close\n";       // this is the                           bit that doesnt work no matter what i do it just doesnt run that section
        }
    }

  12. #12
    Registered User
    Join Date
    Jun 2006
    Posts
    7
    were i put this is the i meant
    this is the bit that doesnt work no matter what i do it just doesnt run that section

  13. #13
    Registered User
    Join Date
    Jan 2005
    Posts
    183
    WTF ?

    What was that last post ? Any one ?
    And what was that about going back to a set place in the code ? Where exactly do you want it to 'go back,' and where do you want it to 'go back' to. I'll try to make a loop for you, and go through it line by line.
    Last edited by Necrofear; 06-30-2006 at 11:38 AM.

  14. #14
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Portugal
    Posts
    7,467
    You are using too many if statements, and some on places where they shouldn't exist.

    Code:
     char a_char;
    
        //removed 'n'. Note really necessary, don't you agree?
        cout<<"Would you like to run this program y = yes\n";
        cin>> a_char;
    
        if (a_char == 'y') //any other char will exit the program
        {
             int age;
    
             cout<<"Please input your age: ";
             cin>> age;
    
             //it makes sure cin is valid. It could be invalid if for instance
             //i tried to input something other than an integer
             if (cin) {  
                 if ( age < 100 )
                    cout<<"You are pretty young!\n";
                 else if ( age == 100 )
                    cout<<"You are old\n";
                 else if ( age > 100 )
                    cout<<"You are really old\n";
             }
             else // if cin is not valid then...
                 cout<<"that is an invalid character, application will now close\n";
    
             cin.ignore();
             cin.get();
    
        }
    
        //main should always return something.
        //the way you had it before, it wasn't all the time.
        return EXIT_SUCCESS;
    The programmer’s wife tells him: “Run to the store and pick up a loaf of bread. If they have eggs, get a dozen.”
    The programmer comes home with 12 loaves of bread.


    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  15. #15
    Registered User
    Join Date
    Jan 2005
    Posts
    7,318
    >> this is the bit that doesnt work no matter what i do it just doesnt run that section

    When the prompt is "Would you like to run this program y = yes n = no", if I type g and hit enter it says, "that is an invalid charector, application will now close".

    The first time you have that error message will never be run, though, because it is the else of if age < 100, age == 100, and age > 100. No matter what age is, it will always be one of those three, so the final else will never be run. You just have to look at the logic of the code to see why it is never called.

Page 1 of 2 12 LastLast
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