I'm having fits with an assignment. Basically, the program is supposed to read in command line arguments and input from a text file and, through a long and convoluted chain of dynamic structures, sort them according to the command line input. The input text file reads as such:
"Smith, John" 100
"Jones, Bob" 90
"Willis, Bruce" 75

The first number is read in by a function that stores the size of a set of arrays. The function I'm having trouble with is the one that takes each line after the first (as it has already been read by cin.get) and stores them into 2 variables. Here is the code:

bool readAndParseDataLineFromCIN (char name [], unsigned int & score)
  int lineIndex = 0, nameIndex = 0;
  char line[MAX_LINE_LENGTH];
  int quoteCount = 0;
  char numbers[MAX_LINE_LENGTH];
  int counter = 0;

	 // if line does not have 2 quote characters file is invalid

  cin.getline (line, MAX_LINE_LENGTH);  //puts the line into an array. Function will //extract name and score into separate things. Nullterm = '/0'
  for (int i; i <= MAX_LINE_LENGTH; i++) //probably garbage right here
	  if (line[i] == '\"')

		  name [i] = line[i];
	  } while (quoteCount == 1 && line[i] != '\"');

	  if (quoteCount == 2 && line[i] == '\"')
		  line[i] = '\0';

		  numbers[counter] = line[i];

		  if (quoteCount == 2 && i == MAX_LINE_LENGTH)
			  numbers[i] = '\0';
	  } while (quoteCount == 2 && line[i] != '\"');


  score = atoi(numbers);

  for (int j = 0; j <= MAX_LINE_LENGTH; j++)
	  cout << name[j];

  cout << score;

  return quoteCount == 2 && strlen (name) > 0;
  //return true;

The idea is to read everything after the quotes from the input file and put the name into an array and the score afterwards into an unsigned int, both of which were passed in the parameters (i.e. '"Smith, John" 100' would be stored into an array as Smith, John and the 100 be stored into an unsigned int). Right now it's returning garbage and I really don't have a clue as to how to get this thing to work properly. I'm sure my loop(s) are horrid, but I really don't know how else to loop this thing properly.

You're making it complicated with two DO loops inside a FOR loop.

Find the first "
Start a while loop until you find the next ", copying the name
Skip past the "
The rest of the line is the number, just use atoi() at that point.

Of course you still have to make sure the second " was found, so you can trap an error.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.