some more newbie help please

This is a discussion on some more newbie help please within the C++ Programming forums, part of the General Programming Boards category; hi , I got a little problem , i want the user in my chat programm to be able to ...

  1. #1
    email for MystWind avatar MystWind's Avatar
    Join Date
    Feb 2005
    Location
    Holland , The Hague
    Posts
    88

    Question some more newbie help please

    hi , I got a little problem , i want the user in my chat programm to be able to type his name , then be able to chat , and chat , till he drops , i tyed to do that with a loop , but its looping the username with it , does the username needs to be in an other loop for this ?cause now it goes like : <username> says : h <username> says i . instead of <username> says : hi

    ok here it is :

    Code:
    #include <iostream>
    using namespace std ;
    #include <stdlib.h>
    #pragma hdrstop
    
    int main ( int , char** )
    {
    unsigned char x = 4 ;
    char pname[64] ;
    unsigned char z = 0 ;
    unsigned char a = 2;
    char says[64] ;
    
    
    cout << endl << "username: " ;
    
    if ( pname != 0 ) {
    
      gets(pname);  
      
    
    while ( a != 0 ) { 
    
    cin >> z ; 
    cout  << pname << " says : " << z  ;
    }
    
    
    
    
    cin.ignore();
    
    return 0 ;
    
    }
    }
    thanks.
    PLay MystWind beta , within two years

  2. #2
    email for MystWind avatar MystWind's Avatar
    Join Date
    Feb 2005
    Location
    Holland , The Hague
    Posts
    88
    quick please :P i'm going on holliday for a week within just a little time.
    PLay MystWind beta , within two years

  3. #3
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,799
    Your input loop is only getting a single character at a time, that is why your output shows up as it does. Let's say you enter "hi" and then press enter. The cin gets the first character 'h' and prints out username says : h. Then we go back to the beginning of the loop where the cin sees that there is stuff still in the input stream and it immediately gets the next character 'i' without waiting for you to type anything and prints out username says : i. When we go back to the beginning of the loop for the third time, there is nothing in the input stream and the cin will wait for some more input.

    You need to get more than just a single character each time through the loop. I'd probably do something like:

    Code:
    #include <string>
    
    ...
    
    string message;
    
    while ( true ) { 
        getline(cin,message);
        if( !message.length() ) break;
        cout  << pname << " says : " << message << endl;
    }
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  4. #4
    email for MystWind avatar MystWind's Avatar
    Join Date
    Feb 2005
    Location
    Holland , The Hague
    Posts
    88
    thanks again man , your the best !
    PLay MystWind beta , within two years

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. newbie: array question :(
    By cstudent in forum C Programming
    Replies: 2
    Last Post: 04-09-2008, 06:46 AM
  2. getting to grips with allegro and ms vc++ (newbie)
    By jimjamjahaa in forum C++ Programming
    Replies: 4
    Last Post: 11-18-2005, 06:49 PM
  3. Newbie in problem with looping
    By nrain in forum C Programming
    Replies: 6
    Last Post: 11-05-2005, 11:53 AM
  4. Some help for a newbie?
    By Ilmater in forum C++ Programming
    Replies: 23
    Last Post: 04-19-2004, 07:44 PM
  5. Newbie Game Develpoers Unite!
    By Telenosis in forum Game Programming
    Replies: 10
    Last Post: 06-22-2002, 02:02 PM

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