Thread: Need help with simple program

  1. #1
    Registered User
    Join Date
    Aug 2007
    Posts
    22

    Need help with simple program

    I combaned to of the exaple's and it's not working I've tryed everthing I can think of and it's still not working.

    Heres my code
    Code:
    
    using namespace std;
    
    int main()
    {
             int thisnumber,
             int cin;
             
             cout int ,<<"Please enter a number: ";
             cin<<thisnumber;
             cin.ignor();
             if (thisnumber > 100){
                            cont<<"You LOST!!! HAHAHAHAH!!!!!!!!!!!\n;"
                            }
             if (thisnumber <100){
                            cont<<"Good job you won A POINTLESS GAME thank you for waisting you time!\n;"
                            }
             cin.get();
    }
    It comples up to int cin; and it stops working and I can't fix it.

  2. #2
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,229
    int cin;
    I don't think this is right. cin is defined in std.

    cin.ignor();
    typo, should be cin.ignore();

  3. #3
    Registered User
    Join Date
    Aug 2007
    Posts
    22
    I dont realy understand what your trying to say. Cin.ignor() is a varable so I though I had to declare it but it still does not want to work..

  4. #4
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    Well first of all, why dump code on us without telling the exact compiler error? If you've got the error message and you can't figure it out, why make it harder on us and make us guess the error as well the solution?

    But as to what your problem in..... Why would you name a variable the same name as cin, which is an object of type..... istream (I think)? Get rid of it. You don't need that variable.

    Code:
    int thisnumber,
    End the declaration with a semicolon, not a comma.

    Also, what the heck is this line?

    Code:
    cout int ,<<"Please enter a number: ";
    Take your time to write programs. This just shows sloppiness in writing any random thing. It means nothing. What I imagine you meant is this:

    Code:
    cout << "Please enter a number: ";
    Code:
    cin<<thisnumber;
    You got the direction wrong. You're reading a number from cin to thisnumber. Hence you need:

    Code:
    cin >> thisnumber;
    Code:
    cin.ignor();
    Mistake, I take it and you want cin.ignore().

    Code:
    cont<<"You LOST!!! HAHAHAHAH!!!!!!!!!!!\n;"
    ...
    cont<<"Good job you won A POINTLESS GAME thank you for waisting you time!\n;"
    cout perhaps?

    And put the semicolons on the end of the line. Not inside the string.

    Your indenting kind of sucks, too. Working on indenting will help you write, debug, and maintain software.

    Furthermore, if your program compiled and executed, what would happen if you entered 100? Nothing would be printed.

    How's this for an example?

    Code:
    #include <iostream>
    
    using std::cout;
    using std::cin;
    
    int main()
    {
             int thisnumber;
    
             cout << "Please enter a number: ";
             cin >> thisnumber;
             
             if(thisnumber > 100)
    	 {
    		 cout << "You LOST!!! HAHAHAHAH!!!!!!!!!!!" << std::endl;
    	 }
             else if(thisnumber < 100)
    	 {
    		 cout << "Good job you won A POINTLESS GAME thank you for waisting you time!" << std::endl;
    	 }
    	 else cout << "You tied.  Maybe try again later." << std::endl;
    
             cin.ignore();
    	 return 0;
    }
    You might need to add an extra cin.ignore() since it might be reading the '\n' or '\r' from the user.

    Lastly, welcome to cboard.

  5. #5
    Registered User
    Join Date
    Aug 2007
    Posts
    22
    Thank you for the help I tried to run it but when I enterd the number and pressed enter the program shut down.. how can I fix this?

  6. #6
    Internet Superhero
    Join Date
    Sep 2006
    Location
    Denmark
    Posts
    964
    http://faq.cprogramming.com/cgi-bin/...&id=1043284385

    As MacGyver said, you might need to add another cin.ignore(); at the end of your program, to make it hold before exiting.

  7. #7
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    Quote Originally Posted by MacGyver View Post
    You might need to add an extra cin.ignore() since it might be reading the '\n' or '\r' from the user.
    Put an extra cin.ignore() before the return.

    In reality, it's not the best way of handling it. You should really clear the input buffer by means of some other function I can't think of at the time, but for now, this is sufficient to see how it works.

  8. #8
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    I believe cin.ignore() (or rather cin.ignore(std::numeric_limits<std::streamsize>::m ax(), '\n')) is the most reliable method of clearing the input buffer.

  9. #9
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,229
    and a cin.get() at the end

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help with a very simple program
    By htdefiant in forum C++ Programming
    Replies: 13
    Last Post: 08-14-2007, 01:27 PM
  2. Using variables in system()
    By Afro in forum C Programming
    Replies: 8
    Last Post: 07-03-2007, 12:27 PM
  3. [Help] Simple Array/Pointer Program
    By sandwater in forum C Programming
    Replies: 3
    Last Post: 03-30-2007, 02:42 PM
  4. simple silly program
    By verbity in forum C Programming
    Replies: 5
    Last Post: 12-19-2006, 06:06 PM
  5. fopen();
    By GanglyLamb in forum C Programming
    Replies: 8
    Last Post: 11-03-2002, 12:39 PM