Moonglow.cpp

Here's Moonglow's format. The text file is composed of words. If a word is a number, then that is a student's score on a question, so you add it to the student's exam score. If the word is not a number, but is the word "NAME", then the next word is the student's name (Moonglow only uses first names -- last names are corporate and impersonal). If the word is "AVERAGE", then you start reading numbers until you read a word that is not a number (or is the end of the file). You average all of those numbers and add that to the score. Since Moonglow is a little scatterbrained, sometimes a number does not follow "AVERAGE." In that case, you ignore the "AVERAGE"

This is the assignment. I haven't completely gotten to the math yet, because currently on this program I am getting tons of compilation errors.

Here is a sample of what it should look like:
"UNIX> cat test-5.txt
NAME Baby-Daisy
AVERAGE 3 4 5 6
AVERAGE 7 8 9
Where's Starrlite!!
UNIX> ./moonglow < test-5.txt
Baby-Daisy 12.5"

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
 #include <iostream>
#include <string>
#include <sstream>
#include <vector>
using namespace std;

int main(){
    vector <string> tokens;
    string s;
    bool hitavg = false;
    bool hitname = false;
    string avgKey = "AVERAGE";
    string nameKey = "NAME";
    string sub, name;
    bool isInt = false, isAvgInt = false;
    string stuName;
    int score, avg, final, finalavg;

    cin >> s;
    istringstream iss(s);

    while(iss){
        iss >> sub;

        tokens.push_back(sub);

    }
    for(unsigned int i = 0; i < tokens.size(); i++) {
        if (hitavg){
            for (int a = i; a < tokens.size(); a ++)
            {
                if (!(tokens[i] >= '0' && tokens[i] <= '9')) return false;
                else avg += tokens[i];
            }


        }
        if (hitname){
            name = tokens[i+1];
        }

        if(tokens[i] == avgKey) hitavg = true;
        else if (tokens[i] == nameKey) hitname = true;
    }





return 0;
    }// end of main
Line 32, your vector contains strings not char
Line 33, you cannot add a string to an integer (You have also not initialised this integer)
So how would I check if an entire string is greater than or = to zero, and how would I add this to avg (which should be initialized to 0).
Follow the instructions given carefully, you do have control over when you need to read in a string vs when you need to read in a number.

After you read a string that is "AVERAGE", is when you can start reading numbers. Apart from this, you should only be reading strings from stdin.

Hint: the istream object (cin) has a method called eof which returns true if cin has reached the end-of-file.

HintHint: Attempting to read a string into an integer returns false.

If you choose to only read strings, then you can use some c++ string-to-int functions to convert the string to an integer and do your testing
Last edited on
Couple followup questions:

How would I convert a read in word to a double or an integer?

I also need to read in numbers before I hit AVERAGE and/or NAME for individual scores (these will be added to the average at the end). Should I have cin read into a string and integer at the same time? Would vectors be best?
Since each file contains only one name, there is no need for vector in this case
The string class contains the following functions which convert a string to integer:
http://en.cppreference.com/w/cpp/string/basic_string/stol
Or double:
http://en.cppreference.com/w/cpp/string/basic_string/stof

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
while (cin >> word)
{
	if (word == "NAME")
	{
		// read the name
	}
	else if (word == "AVERAGE")
	{
                // do average calculation
	}
	else // Found numbers
	{
					
	}
	cin.clear();
}
Topic archived. No new replies allowed.