Thread: Adding Scores from a string array

  1. #1
    Registered User
    Join Date
    Feb 2005
    Posts
    38

    Adding Scores from a string array

    I have to do the read a string, accumulate the scores, and get the average from records.dat.

    The string includes name and test scores.ie John Smith 99 89 88 91.

    My problem is that I can't figure out how to pull the test scores while reading the string so that I can accummulate the scores and find the average.

    Here's my code so far:
    Code:
    #include <conio.h>
    #include <iostream>
    using namespace std;
    #include <fstream>
    #include <string>
     
    int main(){
    	string str[20];
    	int i = 0, j = 0, last;
    	string str1 = "Name		Score1	Score2	Score3	Score4	Average";
    	cout << str1 << endl;
    	ifstream InFile("Records.dat");
    	if(! InFile)
    		{
    		 cout << "Error opening output file" << endl;
    		 getch();
    		 return -1;
    		}
    		while(! InFile.eof()){
    			for(int I = 0; i < 5; i++){
    				getline(InFile, str[i]);
    				cout << str[i] << endl;
    			}
    		}
     
    	InFile.close();
    	//To make console show
    	getch();
    	//return '0' to show that the process ran ok.
    	return 0;
    }

  2. #2
    Registered User
    Join Date
    Aug 2005
    Posts
    1,267
    1. the loop is wrong -- you don't need the "for(I ..." loop at all. But you will probably need some other loops as indicated later. Use getline to read a whole line into a std::string variable, then chop the string up into its individual parts.

    2. You don't need to use an array of strings -- str array. There is no need to keep the individual strings. When a line is read, extract the name, each individual grade, calculate the average, and finally display the line.

    For example, you know that the student's name appears at the beginning of the line and may contain one or more spaces. So just searching the string for the first space is not going to help you in extracting the whole name. Instead, write a loop that will look for the first numeric digit, which will be the beginning of the first grade (assuming the student's name does not contain any numeric digits). At the end of this loop you will have the student's name. Now you can write other similar loops to extract each of the grades.
    Code:
    std::string name;
    int i;
    for(i = 0; i < line.length() && !isdigit(line[i]); ++i)
       name += line[i];
    Last edited by Ancient Dragon; 11-09-2005 at 12:09 PM.

  3. #3
    Registered User
    Join Date
    Feb 2005
    Posts
    38
    I got the error isdigit() cannot convert parameter 1 from std::string to int.

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    In the example, line is a string, so line[i] is a character (that converts to an int automatically). Make sure you are using the [i] to get the ith character from the string.

  5. #5
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    I have to do the read a string, accumulate the scores, and get the average from records.dat.

    The string includes name and test scores.ie John Smith 99 89 88 91.
    Where does the string come from? A file? Will it always be the same format, i.e.: first, last, score1, score2, score3, score4?

    You should always look to ''>>" when reading in data that is separated by spaces. If you know the data is going to be in that format every time, it's simple matter of declaring two string variables and an int array to read in the data. The ">>" operator reads in data sequentially one "word" at a time, where a word is a sequence of adjacent characters. Here is an example:

    dataSource>>first>>last>>score1>>score2>>score3>>s core4;

    Additionally, the general rule is to make your read statement a while loop conditional. You may not realize it, but a statement like:

    dataSource>>first;

    calls a function. The function reads data from the source and puts the data into the variable first. The return value of the function is whatever is on the left side of the ">>" symbol, e.g. dataSource. In a loop conditional, the dataSource object will evaluate to false if for any reason you can't read from the source anymore--like when hitting the end of the file or an error occurs while reading in data.

    If you can't read in any data because of an error, your loop should terminate immediately otherwise you may enter an infinite loop. Checking for eof() isn't the answer. If there's an error while reading in data before hitting eof(), then you won't be able to read in anymore data. Your loop will continue looping because you haven't hit eof yet, but you can't read in data because of the error, so you will never hit eof, and the loop will be infinite.
    Last edited by 7stud; 11-09-2005 at 11:54 PM.

  6. #6
    Registered User
    Join Date
    Feb 2005
    Posts
    38
    Quote Originally Posted by 7stud
    Where does the string come from? A file? Will it always be the same format, i.e.: first, last, score1, score2, score3, score4?

    You should always look to ''>>" when reading in data that is separated by spaces. If you know the data is going to be in that format every time, it's simple matter of declaring two string variables and an int array to read in the data. The ">>" operator reads in data sequentially one "word" at a time, where a word is a sequence of adjacent characters. Here is an example:

    dataSource>>first>>last>>score1>>score2>>score3>>s core4;

    Additionally, the general rule is to make your read statement a while loop conditional. You may not realize it, but a statement like:

    dataSource>>first;

    calls a function. The function reads data from the source and puts the data into the variable first. The return value of the function is whatever is on the left side of the ">>" symbol, e.g. dataSource. In a loop conditional, the dataSource object will evaluate to false if for any reason you can't read from the source anymore--like when hitting the end of the file or an error occurs while reading in data.

    If you can't read in any data because of an error, your loop should terminate immediately otherwise you may enter an infinite loop. Checking for eof() isn't the answer. If there's an error while reading in data before hitting eof(), then you won't be able to read in anymore data. Your loop will continue looping because you haven't hit eof yet, but you can't read in data because of the error, so you will never hit eof, and the loop will be infinite.
    Data comes from a .dat file.

    first last test1 test2 test3 test4

    I think I hit the .eof() loop so I changed to a for() loop.

  7. #7
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    I think I hit the .eof() loop so I changed to a for() loop.
    Wrong approach. Read my post again:

    Additionally, the general rule is to make your read statement a while loop conditional.
    For instance,

    Code:
    while(inFile>>data1>>data2)
    {
    
    
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. String Class
    By BKurosawa in forum C++ Programming
    Replies: 117
    Last Post: 08-09-2007, 01:02 AM
  2. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 05:45 PM
  3. can't assign proper values to an array of string
    By Duo in forum C Programming
    Replies: 1
    Last Post: 04-04-2005, 06:30 AM
  4. Linked List Help
    By CJ7Mudrover in forum C Programming
    Replies: 9
    Last Post: 03-10-2004, 10:33 PM
  5. Warnings, warnings, warnings?
    By spentdome in forum C Programming
    Replies: 25
    Last Post: 05-27-2002, 06:49 PM