Learning C++ - Coded simple program which crashes

This is a discussion on Learning C++ - Coded simple program which crashes within the C++ Programming forums, part of the General Programming Boards category; I coded this to test FOR loops, but whenever I enter YES it crashes. Code: // Includes the basic IO ...

  1. #1
    Registered User
    Join Date
    Sep 2004
    Posts
    1

    Learning C++ - Coded simple program which crashes

    I coded this to test FOR loops, but whenever I enter YES it crashes.
    Code:
     // Includes the basic IO functions.
     #include <iostream.h>
     #include <string.h>
     
     // This program is coded simply to test the FOR loop in Visual C++.
     
     int main(void) {
     
     	char * response;
     
     	cout << "Ready to begin? Type YES if you are.";
     
     	cin >> response;
     
     	if (response == "YES" || response == "yes") {
     	
       cout << "This is the loop:" << endl;
     	
       for (int x = 0; x < 100; x++) {
       	
       	cout << x << endl;
       }
       
       return 0;
     
     	}
       
     	else {
       
     	cout << "Very well. Program terminating.";
       
     	}
     
     	return 0;
     
     }

  2. #2
    Arggggh DeepFyre's Avatar
    Join Date
    Sep 2004
    Posts
    227
    seems like visual c++ is quite messed up
    that ryback guys is also having problems with visual c++
    Keyboard Not Found! Press any key to continue. . .

  3. #3
    i dont know Vicious's Avatar
    Join Date
    May 2002
    Posts
    1,200
    Your trying to put a string of text into a char pointer.
    Therefore it crashes because there is no memory allocated for it.

    EDIT: Also your not using standard headers.

    try this

    EDITED AGAIN, I tested it and this works

    Code:
    #include <iostream> //+--Changed to standard header--+
    #include <string>
    using namespace std;
     
    int main(void) {
     
        string response;
     
        cout << "Ready to begin? Type YES if you are.";
     
        cin >> response;
     
        if (response == "YES" || response == "yes") {
     	
            cout << "This is the loop:" << endl;
     	
            for (int x = 0; x < 100; x++) {
       	
                cout << x << endl;
    
            }
       
        }
       
        else {
       
            cout << "Very well. Program terminating.";
       
        }
     
        cin.ignore();
        cin.get();
        return 0;
     
    }
    Last edited by Vicious; 09-08-2004 at 10:02 PM.
    What is C++?

  4. #4
    Registered User
    Join Date
    Jul 2004
    Posts
    98
    @ Vicious :
    cin.ignore();
    cin.get();
    Why using cin.ignore , how does it work?
    Thanks.

  5. #5
    Registered User
    Join Date
    Jul 2004
    Posts
    98
    haha, I have read next post , find this answer:
    The reason is that when you extract a, b, and c from the input stream, the newline from when you hit enter is still there. The cin.get() waits for the next newline, but since there is already one there your console closes. The call to cin.ignore() above fixes that specific problem by ignoring that first newline and allowing the cin.get() to wait for the next one. You could also put cin.get() twice or cin.ignore() twice to achieve the same effect.
    author: jlou

  6. #6
    I'll take you down! polonyman's Avatar
    Join Date
    Sep 2004
    Posts
    50

    Smile easy

    mate u allocate response as a char. char is used to store a single letter variable and u tryed to make it store 3 (yes). so just make it say(please enter y or n) unless u know hot to use arrays and strings

  7. #7
    Registered User jlou's Avatar
    Join Date
    Jul 2003
    Posts
    1,088
    Quote Originally Posted by toysoldier
    haha, I have read next post , find this answer:
    The reason is that when you extract a, b, and c from the input stream, the newline from when you hit enter is still there. The cin.get() waits for the next newline, but since there is already one there your console closes. The call to cin.ignore() above fixes that specific problem by ignoring that first newline and allowing the cin.get() to wait for the next one. You could also put cin.get() twice or cin.ignore() twice to achieve the same effect.
    author: jlou
    You forgot the rest of that quote! Basically:

    I'd prefer this code, because more potential problems can be avoided this way.
    Code:
    #include <iostream>
    #include <string>
    #include <limits>
    
    int main()
    {
        std::string response;
        std::cout << "Ready to begin? Type YES if you are.";
        std::cin >> response;
    
        // ...
    
        std::cin.ignore(std::numeric_limits<int>::max(), '\n');
        std::cin.get();
        return 0;
    }
    That ignores all characters in the input stream up to and including the newline - which helps in case the user accidentally types in characters after the stored response.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Utter newb?: Program crashes after input
    By deductible in forum C++ Programming
    Replies: 5
    Last Post: 12-13-2008, 10:27 PM
  2. Replies: 5
    Last Post: 11-27-2005, 09:50 PM
  3. Need help with simple, simple program.
    By LightsOut06 in forum C Programming
    Replies: 5
    Last Post: 09-01-2005, 09:31 PM
  4. simple frontend program problem
    By gandalf_bar in forum Linux Programming
    Replies: 16
    Last Post: 04-22-2004, 07:33 AM
  5. Program crashes and I can't figure out why
    By Unregistered in forum C Programming
    Replies: 6
    Last Post: 09-19-2001, 06:33 PM

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