Problems with commas as input

This is a discussion on Problems with commas as input within the C++ Programming forums, part of the General Programming Boards category; Hi, I'm quite new at C++ and I'm having a problem that's been bugging me for awhile now. I want ...

  1. #1
    Registered User
    Join Date
    Feb 2003
    Posts
    2

    Problems with commas as input

    Hi, I'm quite new at C++ and I'm having a problem that's been bugging me for awhile now. I want to read a bunch of numbers like so: 1,2,1,4,5,6

    If I input the above like so: "1 2 1 4 5 6", (while commenting the cin>>junk, it works perfectly. Whenever I try to use commas, however, the program crashes. If anyone could tell me why, I'd be grateful. Here's my input code:

    Code:
    void input()
    {
    	int x=0;
    	int player[20]; //Array - Player statistics
    	int enemy[20]; //Array - Enemy statistics
    	int dice[3]; //Array - Dice
    	int size=0; //Number of squares with markers (player)
    	int size2=0; //See above, but for enemy
    	char junk; //Takes in the junk commas in the input
    
    	for(x=0;x<20;x++) //Sets all elements of the array equal to zero.
    	{
    		player[x]=0;
    		enemy[x]=0;
    	}
       cout<<"\n\nInput: ";
    	cin>>player[0];
    	size = player[0]*2;
    
    	for(x=1;x<=size;x++) //Sets player's numbers (inputted)
    	{
    		cin>>junk;
    		cin>>player[x];
    	}
    
    	cin>>junk;
    	cin>>enemy[0];
    	size2 = enemy[0]*2;
    
    	for(x=1;x<=size2;x++)
    	{
    		cin>>enemy[x];
    		cin>>junk;
    	}
    
    	for(x=0;x<3;x++)
    	{
    		if(x!=2)
    		{
    			cin>>dice[x];
             cin>>junk;
    		}
    		else
    		{
    			cin>>dice[2];
    		}
    	}
    
    //I call another function and I do close the brackets to void-function input down here eventually.
    Thank you.

  2. #2
    _ Munkey01's Avatar
    Join Date
    Dec 2002
    Posts
    280
    Could it be because a comma isn't an integer? (besides it's ASCII value)

  3. #3
    Registered User
    Join Date
    Feb 2003
    Posts
    2
    Yeah, that's why I periodically scatter cin>>junk; (declared as a char) throughout it. The input would consist of an int, comma, int, comma, int, comma, so on and so forth. I can't find a fault in my placement of cin>>junk;, that's my problem .

  4. #4
    Registered User rmullen3's Avatar
    Join Date
    Nov 2001
    Posts
    330

    ~

    Cin waits for a return. What you want to is get a line (use getline and a char array), then run through it, ignoring the comments, picking out the numbers.

    The functions I recommend... getline, sscanf. That should be enough to do this, although there's better ways.
    "He who makes a beast of himself, gets rid of the pain of being a man." Dr. Johnson

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Bad output -- possible input buffer problems
    By jake123 in forum C Programming
    Replies: 8
    Last Post: 02-18-2008, 02:36 PM
  2. Need some help with C program writing
    By The_PC_Gamer in forum C Programming
    Replies: 9
    Last Post: 02-12-2008, 08:12 PM
  3. Problems with input and fflush
    By edugarcia in forum Linux Programming
    Replies: 1
    Last Post: 11-24-2004, 12:52 PM
  4. Custom Made Safe Input Function
    By Beast() in forum C Programming
    Replies: 6
    Last Post: 08-21-2004, 10:19 PM
  5. White space problems in file input
    By cxs00u in forum C++ Programming
    Replies: 4
    Last Post: 03-20-2002, 10:06 PM

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