program problems

This is a discussion on program problems within the C++ Programming forums, part of the General Programming Boards category; ok i want the user to enter a name and have the program save it to a array Code: void ...

  1. #1
    Registered User
    Join Date
    Oct 2001
    Posts
    224

    program problems

    ok i want the user to enter a name and have the program save it to a array

    Code:
    void NewGame()
    {	
    	system("clear");
    	cout<<"what would you like to have your name as?"<<endl;
    	cin.getline(Name,10,' ');
    	cout<<Name;
    	game();
    }
    but were i have ' ' i had to put that to get it to do anything... when i didnt have it no matter what i pressed it didnt move any farther in the program it just stuck at entering input... after i put the ' ' i have to put a space at the end of the name for it to work... so i though why dont i put '\n' there so when they press enter it would keep going but that didnt work either.... and it doesnt work when i dont have anything there either...

    thx for any help

  2. #2
    Registered User axon's Avatar
    Join Date
    Feb 2003
    Posts
    2,572
    hmm '\n' should work just fine, but why are you using getline anyways? is Name a string or an array?

    if Name is indeed a string just have cin>>Name;

    also Name is neither passed into this function or declared inside of it...

    edit:: err, sorry I ahven't used getline in such a long time that I forgot it wont work with a string at all.
    Last edited by axon; 09-28-2004 at 04:36 PM.

    some entropy with that sink? entropysink.com

    there are two cardinal sins from which all others spring: Impatience and Laziness. - franz kafka

  3. #3
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,246
    So the following code doesn't pass the getline() call in the following code for you?

    Code:
    int main()
    {
    	char Name[10];
    	cout<<"what would you like to have your name as?"<<endl;
    	cin.getline(Name,10);
    	cout<<Name;
    	return 0;
    }

  4. #4
    Registered User
    Join Date
    Oct 2001
    Posts
    224
    Name is an array of 20 objects (Name[20])

    ok i changed it to just cin>>Name; but that doesnt get me any farther... now the problem is it wont do anything after i enter a name and press enter... it just goes to the next line (does it make any difference that im using linux??) were i can type more info. here is the code

    Code:
    void NewGame()
    {	
    	system("clear");  //would clrscr() work?
    	cout<<"what would you like to have your name as?"<<endl;
    	cin.getline(Name,10, ' ');
    	cout<<Name;
    	game();
    }
     ...
    
    void game()
    {
    	cout<<"this far";
    	
    	if (maplvl != 1);
    	{
    		cout<<"im here";
    		if (maplvl == 2)
    		{
    			fin.open("map2.txt", ios::app);
    			while(!fin.eof());
    			{
    				cout<<"im here2";
    				fin >> szMap[x][y];
    				cout<<szMap[x][y]<<endl;
    				x++;
    			}
    			mapLoad = true;
    		}
    	}
    }
    im still working with the file input output stuff (its not working either) so the game() function is far from complete.. im just working on getting the program to get that far.

    thx

  5. #5
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Using an array of strings to hold names:
    Code:
    #include <iostream>
    
    int main(void)
    {
      std::string s[2];
      
      for (int i = 0; i < 2; i++)
      {
        std::cout <<"Enter name " <<i <<":" <<std::endl;
        std::getline (std::cin, s[i]); // Do error checking !
      }
      
      for (int i = 0; i < 2; i++)
      {
        std::cout <<"Name " <<i <<" is " <<s[i] <<std::endl;
      }
      
      return(0);
    }
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  6. #6
    Registered User
    Join Date
    Oct 2001
    Posts
    224
    thx for the code but isnt it easier to just put
    using namespace std;

    instead of std:: infront of everything?

  7. #7
    Registered User
    Join Date
    Oct 2001
    Posts
    224
    ok this is what i got now
    Code:
    void NewGame()
    {	
    	system("clear");
    	cout<<"what would you like to have your name as?"<<endl;
    	getline(cin,Name);
    	cout<<Name;
    	game();
    }
    
    ...
    but it still doesnt make it to the game() function it just stalls at the input part... whatever i put nothing happens

  8. #8
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >>but isnt it easier to just put
    >>using namespace std;
    Whatever ....
    http://faq.cprogramming.com/cgi-bin/...&id=1043284351
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  9. #9
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >> it just stalls at the input part
    Post something that we can compile to allow us to see your problem fully.
    Code:
    #include <iostream>
    
    using namespace std;
    
    void game(void)
    {
      cout << "Game run" << endl;
    }
    
    void NewGame(void)
    {
      string  Name;
      system("clear");
      cout << "what would you like to have your name as?" << endl;
      getline(cin, Name);
      cout << Name;
      game();
    }
    
    int main(void)
    {
      NewGame();
    }
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  10. #10
    Registered User
    Join Date
    Oct 2001
    Posts
    224
    yes that is basicaly it but it doesnt work on my computer.. if i use cin.getline(Name, 19) it just skips over getting the user input... but if i use cin.getline(Name, 19, ' ') it waits till i enter a name then a space and then prints out what i entered then stopps.

    im starting to get really fustrated...

    well here is the whole source (very rough and uncomplete)
    Attached Files Attached Files

  11. #11
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Code:
    void game()
    {
    	cout<<"this far";
    	
    	if (maplvl != 1);
    Extra ; at the end of that last line?
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  12. #12
    Registered User
    Join Date
    Oct 2001
    Posts
    224
    well that fixed alot but also brought more problems lol

    ok now i get to the points were output is "this far" "im here" and "im here2" but im here 2 loops untill there is a segmentation fault. which is good cause im progressing

    ok now for the next question: do you know of any good tutorials on file i/o and storing a file that looks like
    xxxx
    xxxx
    xxxx
    xxxx

    into an array? because what i got isnt working

  13. #13
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    So, I changed :

    >char Name[20];
    to be
    >string Name;

    and
    >cin.getline(Name, 19,' ');
    to be
    >getline(cin, Name);
    Code:
    what would you like to have your name as?
    hammer
    hammer
    this farim here

    >>fin.open("map2.txt", ios::app);
    Get that worked before using it.

    >>while(!fin.eof());
    Another extra ; character.
    Plus, read the FAQ here:
    http://faq.cprogramming.com/cgi-bin/...&id=1043284351
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Program Plan
    By Programmer_P in forum C++ Programming
    Replies: 0
    Last Post: 05-11-2009, 01:42 AM
  2. Problems with DLLEXPORT while updating a program
    By pirata in forum C++ Programming
    Replies: 3
    Last Post: 09-05-2008, 01:00 PM
  3. having problems with my card program
    By mac025 in forum C Programming
    Replies: 4
    Last Post: 01-31-2006, 03:26 PM
  4. structure problems in windows program.
    By Bajanine in forum Windows Programming
    Replies: 3
    Last Post: 04-19-2004, 06:18 PM
  5. fopen();
    By GanglyLamb in forum C Programming
    Replies: 8
    Last Post: 11-03-2002, 11:39 AM

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