Problem with code, help please.

This is a discussion on Problem with code, help please. within the C++ Programming forums, part of the General Programming Boards category; Hi. After writing this code on Dev-C++: Code: #include <iostream> using namespace std; int main() { int name; cout<<"Please input ...

  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
    20,981
    Go read the FAQ.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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,189
    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.
    Until you can build a working general purpose reprogrammable computer out of basic components from radio shack, you are not fit to call yourself a programmer in my presence. This is cwhizard, signing off.

  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
    20,981
    What's the code for delay?
    Have you not read the FAQ?
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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 12:56 PM. Reason: Wishing LaserLight a happy birthday ;)

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    20,981
    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().
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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
    20,981
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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をこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,184
    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.
    For information on how to enable C++11 on your compiler, look here.
    よく聞くがいい!私は天才だからね! ^_^

  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をこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,184
    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.
    For information on how to enable C++11 on your compiler, look here.
    よく聞くがいい!私は天才だからね! ^_^

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

    Code:
    getline(name);
    -OR-

    Code:
    std::getline(name);

Page 1 of 2 12 LastLast
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, 10:50 AM
  2. Problem with game code.
    By ajdspud in forum C++ Programming
    Replies: 5
    Last Post: 02-14-2006, 05: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, 04:47 PM
  5. Help with code for simple Y2K problem
    By Mule in forum C++ Programming
    Replies: 3
    Last Post: 03-05-2003, 11:53 PM

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