Thread: Need help with a very, very basic C++ question

  1. #1
    Registered User
    Join Date
    Jan 2012
    Posts
    2

    Need help with a very, very basic C++ question

    Hi all, I'm new here. I was doing the C++ tutorial on the main website and, two lessons in, found the if statements.

    Now I love if statements so I started messing around to see what I could do with them. I managed to get a multiple part if statement program running but I could not for the life of me find a way to get it to repeat its query and receive new input.

    My code is as follows:
    Code:
    #include <iostream>
    using namespace std;
     
    int main ()
    {
     int a;
     cout<<"Please multiply 5 and 10:";    // Asks for answer 
     cin>> a;                          // The input is put in answer
        cin.ignore();    // Throw away enter
      {
      
     
      if ( a==50 ) 
      {                  
         cout<<"Congratulations, you can do basic arithmetic."; 
      }
      else if ( a<50 ) 
      {            
         cout<<"Seriously? You can't do that problem? You suck.\n";           
      }
      else if ( 100>a && a>50 ) 
      {
        cout<<"Dear god man this isn't that hard!\n";     
      }
      else if ( 200>a && a>100 ) 
      {
        cout<<"Alright now you're just intentionally ........ing me off.\n";
      }
      else if (a>=200)
      {
       cout<<"Ok, I'm done. Get out of here stalker.";
      }
      }
      cin.get();
    }
    Now this program works, but it only works once. After entering a single value and pressing enter, it ends upon pressing enter again.

    I've tried adding cout<<"Please multiply 5 and 10:"; and cin.ignore(); to the end of each if statement, I've tried looking a few lessons ahead at loops but couldn't figure out a way to adapt one to do this. While adding the above two lines achieved some of what I wanted, it still didn't accept new input.

    To reiterate, what I want it to do is functionally repeat everything after int main() EXCEPT if "else if (a>=200)" is true.

    Thanks in advance,

    GA

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Carry on reading the tutorial until you come across "while" and "for" loops.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Jan 2012
    Posts
    2
    Quote Originally Posted by Salem View Post
    Carry on reading the tutorial until you come across "while" and "for" loops.
    Alright, I've done that. Unfortunately I still don't know how to make the program refer to code for further instructions instead of simply computing the next number in the sequence. The tutorial illustrates one very simple operation to perform in a loop and I want to do something more complex.

    This is what I have now:
    Code:
    #include <iostream>
    
    using namespace std;
    
    
    int main()
    
    
    {int a=50;
    cout<<"Please multiply 5 and 10:";    // Asks for answer 
    	cin>> a;                          // The input is put in answer
        cin.ignore();				// Throw away enter
      {
      
      if ( a==50 ) 
      {                  
         cout<<"Congratulations, you can do basic arithmetic.";
      }
    }
    
    
      {for (a=50;a=!50;)
    
    
      {if ( a<50 ) 
      {            
         cout<<"Seriously? You can't do that problem? You suck.\n";           
      }
    
    
      else if ( 100>a && a>50 ) 
      {
        cout<<"Dear god man this isn't that hard!\n";     
      }
    
    
      else if ( 200>a && a>100 ) 
      {
    	   cout<<"Alright now you're just intentionally ........ing me off.\n";
      }
    
    
      else if (a>=200)
      {
    	  cout<<"Ok, I'm done. Get out of here stalker.";
      }
      }
      cin.get();
      }
    }
    Is there any help I can get or should I just google it?

  4. #4
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,273
    Very simply, listen to Salem. Also, taking initiative here and showing how you hae taken initiative (i.e. googling yourself) will more than likely help you more so that you can perform independent research, and if we see you have searched we might be more inclined to assist.

    Here's a simple framework:
    Code:
    #include <iostream>
    
    int main() {
      for ( int i=0; i<10; i++ ) {
        std::cout<< i << "\n"; 
      }
    
      return 0; // Always return. See Salem's avatar
    }
    Now try to merge the two codes to do what you want.

  5. #5
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    guardianangel42, first off, you need to improve that indentation.
    The problem is that you do not (yet) understand how to express logic in a programming language. What I recommend that you do is read up on flowcharts. They will be an extremely helpful tool in the beginning of your programming career.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 03-23-2011, 09:00 AM
  2. Basic if else question
    By benrogers in forum C Programming
    Replies: 3
    Last Post: 02-06-2011, 02:39 PM
  3. some basic question about C
    By winggx in forum C Programming
    Replies: 5
    Last Post: 03-09-2010, 10:11 PM
  4. Help if basic question
    By Sshakey6791 in forum C++ Programming
    Replies: 9
    Last Post: 02-28-2009, 04:00 PM
  5. Very basic question
    By something___ in forum C Programming
    Replies: 13
    Last Post: 12-08-2004, 02:53 PM