I'm a newbie to C++ and figured this would be a good place to ask for guidance.

I need to write a program that reads input from a txt file which contains "grades" in numeric format, I.E. 92, 55, 88 and take those grades and calculate the average.

Now my program uses and input stream and loops through the file contents one character at a time, the values are stored in a CHAR variable called "next".

Basically I'm wondering how I can take a grade which could be 1 to 3 characters and concatenate it, then in turn convert into an INT so I may produce the average with it.

Here is how the txt file is formatted:

lastname firstname grade1 grade2 grade3 etc. etc.

doe john 100 84 82 93 45 55 82
blue joe 28 48 98 84 93 100 89 28 84 99

Since I only take in one CHAR at a time, I need to combine the chars before the next space occurs. I then would need to tally the grades all together into an INT so I may average it out.

Any help would be most appreciated!

Here's a hint. Given a character that represents a digit, you can subtract '0' from the character and get the digit value. '0' - '0' is 0, '1' - '0' is 1, etc... With that you can use this expression to build an integer value from a sequence of character digits:

grade = 10 * grade + ( ch - '0' );

Why not use doubles or floats to get your grades? Then you could set it up something like this.

int count = 0;  //Used to get the amount of information for each row from the file.
ifstream fin; //The internal name for the file called clouds.
fin.open("clouds.txt"); // opens the file call clouds.txt
//This loop is to make sure the file opend right and is working.
if (fin.fail())
{
cout <<"Did not open.";
}else{}
//This loop populates the array with information from the file.
for(int r = 0; r < 5;r++)
{
fin >> count;
for(int c = 0; c < count; c++)
{
fin >> cloudsA[r][c];
}
}
fin.close();    //Closes the file.
}

Here's a hint. Given a character that represents a digit, you can subtract '0' from the character and get the digit value. '0' - '0' is 0, '1' - '0' is 1, etc... With that you can use this expression to build an integer value from a sequence of character digits:

grade = 10 * grade + ( ch - '0' );

One problem, since I'm reading in one character at a time, I need to concatenate each char to get the grade. For instance a grade of 100: If I were to convert 1 into a digit, how could I concatenate the remaining 00 to give me 100?

It's for a school project and they specifically want to read in one CHAR at a time. Also the file contains the first and last name which needs to be read in.

>One problem
There's no problem. You simply haven't tried that solution and discovered that it does exactly what you want:

#include <iostream>

int main()
{
  const char stream[] = "100";
  int grade = 0;

  for ( int i = 0; i < 3; i++ )
    grade = 10 * grade + ( stream[i] - '0' );

  std::cout<<"Value: "<< grade <<'\n'
    <<"Squared: "<< grade * grade <<'\n';
}

tehprince

Okay your getting the persons name (first and last) and their grade. The grade being a number(98,76). Now after you get the grade (number) You have to turn it into a letter(A) grade before you add them together. Then you have to turn them back into numbers?
Is this right?

>Is this right?
Probably not. I don't see mention anywhere of calculating a letter grade, only concatenating digits into a real integer that can be used to calculate an average.

>Is this right?
Probably not. I don't see mention anywhere of calculating a letter grade, only concatenating digits into a real integer that can be used to calculate an average.

That's correct.

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.