Thread: Problem with code, help please.

  1. #1
    Registered User
    Join Date
    Jan 2008
    Posts
    32

    Problem with code, help please.

    Hi. After writing this code on Dev-C++:


    Code:
    #include <iostream>	
    
    using namespace std;
    		
    int main()                            
    {
      int name;                            
      
      cout<<"Please input your name: ";    
      cin>> name;                  
      cout<<"Hi! " + name; 
    }
    After I write my name and Press Enter the program closes. Why is that? Any help is greatly appreciated.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Go read the FAQ.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Malum in se abachler's Avatar
    Join Date
    Apr 2007
    Posts
    3,195
    Because thats the way you wrote it. Theres nothing in the program after it prints the message, so it exits. You should probably put some sort of delay in there. And you should read the FAQ.

  4. #4
    Registered User
    Join Date
    Jan 2008
    Posts
    32
    Ok. What's the code for delay?

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    What's the code for delay?
    Have you not read the FAQ?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    Registered User
    Join Date
    Jan 2008
    Posts
    32
    Hi. I figured out the delay. But now it isn't reading my name.

    Code:
    #include <iostream>	
    #include <stdio.h>
    
    using namespace std;
    
    void myflush ( FILE *in )
    {
      int ch;
    
      do
        ch = fgetc ( in ); 
      while ( ch != EOF && ch != '\n' ); 
    
      clearerr ( in );
    }
    
    void mypause ( void ) 
    { 
      printf ( "Press [Enter] to continue . . ." );
      fflush ( stdout );
      getchar();
    } 
    
    int main()                            
    {
    
    
      int name;                            
      
      cout<<"Please input your name: ";    
      cin>> name;      
      cin.ignore();            
      cout<<"Hi! " + name; 
      myflush ( stdin );
      mypause();
    
      return 0;
    
    }
    Please bare with me, I'm a newb at this.

    It should be saying:

    Hi! Fred but it isn't it's just skipping to the part where it says "press [Enter] to continue..."

    Happy birthday laserlight.
    Last edited by Apocalyptic_end; 01-25-2008 at 01:56 PM. Reason: Wishing LaserLight a happy birthday ;)

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    For starters, I suggest using either C style I/O or C++ style I/O, but not both at the same time.

    Consequently, I would rewrite myflush() and mypause() as:
    Code:
    void myflush(std::istream& in)
    {
        in.clear();
        in.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
    }
    
    void mypause()
    {
        std::cout << "Press [Enter] to continue . . .";
        myflush(std::cin);
        std::cin.get();
    }
    You would then only need to call mypause().
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  8. #8
    Registered User
    Join Date
    Jan 2008
    Posts
    32
    Ugh, thanks.


    I'm sorry for using your time for a quite easy question.

    But. What's the difference between C style I/O and C++ style I/O?

  9. #9
    Registered User
    Join Date
    Jan 2008
    Posts
    11
    You could call either
    Code:
    std::cin.get();
    or
    Code:
    system("PAUSE");
    right before the return 0; at the end of main().

    Both of these will stall the program to wait for any user input before closing. I only know for sure that the latter one works in Dev-C++, I have heard it doesn't work on all compliers.

  10. #10
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    What's the difference between C style I/O and C++ style I/O?
    A matter of standard library functions and programming idioms. Other than that it may be necessary to call sync_with_stdio() when mixing usage of C and C++ style I/O, but I am not sure of that at all.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  11. #11
    Registered User
    Join Date
    Jan 2008
    Posts
    32
    I found it I mixed up Java Programming with C++ Programming

  12. #12
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    You are reading a string into a variable that holds an integer (number). How does that work? It doesn't. You need to read into a string. For that, you can use std::string.
    I also recommend getline instead of cin >> since it will read "John Smith" while cin >> would only read "John" (and leave crap in the input buffer).
    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.

  13. #13
    Registered User
    Join Date
    Jan 2008
    Posts
    32

    Ok

    Ok Here is my new code:

    Code:
    #include <iostream>	
    #include <stdio.h>
    
    
    using namespace std;
    
    void myflush(std::istream& in)
    {
        in.clear();
        in.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
    }
    
    void mypause()
    {
        
        myflush(std::cin);
        std::cin.get();
    }
    int main(void)                           
    {
    
    
      string name;                            
      
      cout<<"Please input your name: ";    
      cin.getline( name );  
      cout<<"Hi! " << name;
      
      
      mypause();
      return 0;
    
    }
    Now it works when I use cin >> name.

    But not when I use cin.getline( name ); . Why not?

  14. #14
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Not std::cin.getline, std::getline.
    std:::cin.getline takes a char* as argument, while std::getline takes std::string as argument (plus a reference to a ifstream object I believe, to which you should specify cin).
    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.

  15. #15
    Registered User
    Join Date
    Jan 2008
    Posts
    32
    Ok so should my code be:

    Code:
    getline(name);
    -OR-

    Code:
    std::getline(name);

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Code problem
    By sybariticak47 in forum C++ Programming
    Replies: 9
    Last Post: 02-28-2006, 11:50 AM
  2. Problem with game code.
    By ajdspud in forum C++ Programming
    Replies: 5
    Last Post: 02-14-2006, 06:39 PM
  3. problem with selection code
    By DavidP in forum Game Programming
    Replies: 1
    Last Post: 06-14-2004, 01:05 PM
  4. Replies: 5
    Last Post: 12-03-2003, 05:47 PM
  5. Help with code for simple Y2K problem
    By Mule in forum C++ Programming
    Replies: 3
    Last Post: 03-06-2003, 12:53 AM