Question

I am getting an error that the variable num was not declared in the scope on...

I am getting an error that the variable num was not declared in the scope on the line of code that has; while(num != 5).

Do you know how to fixe this error?

#include <iostream>

#include <iomanip>
#include <string>
#include<fstream>
#include <cstring>
using namespace std;

//Declare the structure
struct Games
{
string visit_team;
int home_score;
int visit_score;
};

// Function prototypes
int readData(Games *&stats);
int menu();
void pickGame(Games *&stats, int size);
void inputValid (Games *&stats, int size);
void homeTotal(Games *&stats, int size);
void visitTotal(Games *&stats, int size);
void displayData(Games *&stats, int size);

int main()
{

//Declare a pointer to the Game structure
Games *stats = nullptr;

//Read in the data from the games.txt text file
//Use the input valid to make sure none of the values are negative
int size =readData(stats);
inputValid(stats, size);

//Create a do-while loop that will continue to iterate through the menu
//until the user decides to exit the program
do
{
//call the menu function, which will display the choices to the user
//and store their choice in a variable
int num = menu();
//Break the choices into cases and complete out the functions accordingly
switch(num)
{
case 1:
displayData(stats, size);
break;
case 2:
pickGame(stats, size);
break;
case 3:
homeTotal(stats, size);
break;
case 4:
visitTotal(stats, size);
break;
case 5:
break;
default:
cout << "Invalid input.\n" <<endl;
}
} while(num != 5);


return(0);
}
//Dynamically allocate the the structure array and read the information into the array
int readData (Games*&stats)
{
int size;
//Games *stats = nullptr;
stats = new Games[size];
stats->visit_team;
stats->home_score;
stats->visit_score;

//open a file to read the games.txt file
ifstream inFile;
inFile.open("games.txt"); //open the text file salsa
if (!inFile)
{
cout << "Error opening data file!\n"; // if the file does not open send an error message to the user

exit(102); //exit the program
}
cout << "Reading data from file \n\n";
inFile >> size;
inFile.ignore();
for (int i = 0; i < size; i++)
{

getline(inFile, stats[i].visit_team);
inFile >> stats[i].home_score;
inFile >> stats[i].visit_score;
inFile.ignore();
}
inFile.close();
return(size);
}
void inputValid (Games *&stats, int size)
{
ifstream inFile; //define infile and outfile
ifstream outFile;
inFile.open("games.txt"); //open the games.txt file
for (int i=0; i< size; i++)
{
if (stats[i].home_score < 0) //Test the value for negative values
{
cout << "Negative values are invalid and will be set to zero.\n\n";
stats[i].home_score = 0; //Set the negative values to 0
outFile >> stats[i].home_score; //write over the negative value as 0 in the games.txt file
}
if (stats[i].visit_score <0)
{
cout << "Negative values are invalid and will be set to zero.\n\n";
stats[i].visit_score = 0; //Set the negative values to 0
outFile >> stats[i].visit_score;
}
}
//Close the file
inFile.close();

}
//Display the menu driven interface for the users to choice which functions they would
//like the program to carry out
int menu()
{
//initialize the variable num to zero
int num = 0;

cout <<"1: Print the information for all the games, in table form." <<endl;
cout <<"2: Print the information for a specific game." << endl;
cout <<"3: Print the total points scored during the season for the home team." << endl;
cout <<"4: Print the total points scored against the home team during the season." << endl;
cout <<"5: Exit the program." << endl;
cout <<"Please enter your choice:" << endl;
cin >> num;
//return the value of num to the main function
return(num);

}
//this function will display the statistics for a particular
//game based on the team name the user inputs
void pickGame(Games *&stats, int size)
{
string name;
//Get the search string from the user
//cin.ignore();
cout << "Enter a specific team name(in caps) for their statistics: ";
cin.ignore();
getline(cin, name);

int location;

// loop through the array of structures
// for each structure the plan is to use .find on the visit_team field
for(int i = 0; i <size; i ++)
{
location = stats[i].visit_team.find(name, 0); //figure out what it returns when it has found the location
if( location >= 0)
{
cout <<"Team: " << stats[i].visit_team << endl << "Home score: "<< stats[i].home_score << endl << "Visit score: " << stats[i].visit_score << endl;
}
}
}
//this function will tally the total number of points the home team has scored
//during the season and display the total
void homeTotal(Games *&stats, int size)
{
int count = 0;
//Calculate the total points scored during the season for the
//home team.
for(int i =0; i <size; i++)
{
count += stats[i].home_score;
}
cout << "The total points scored during the season for the home team was: " << count << endl;
}
//This function will tally the total point scored against the home team for the
//duration of the season
void visitTotal(Games *&stats, int size)
{
int count = 0;
//Calculate the total points scored during the season for the
//home team.
for(int i =0; i <size; i++)
{
count += stats[i].visit_score;
}
cout << "The total points scored against the home team during the season was: " << count << endl;
}
//This function will display all the data in a table form for the user
void displayData(Games *&stats, int size)
{
cout << fixed;
cout << setw(17)<< left<< "Team name: " << setw(26) << right<< " Home score: "<< setw(10)<<right<< "Visiting score:"<< endl;
for(int i =0; i < size; i++)
{
cout << setw(17) << left<<stats[i].visit_team <<setw(18)<<right <<stats[i].home_score << setw(18)<< stats[i].visit_score << endl;
}
}

0 0
Add a comment Improve this question Transcribed image text
Answer #1



#include <iostream>

#include <iomanip>
#include <string>
#include<fstream>
#include <cstring>

using namespace std;

//Declare the structure
struct Games {
    string visit_team;
    int home_score;
    int visit_score;
};

// Function prototypes
int readData(Games *&stats);

int menu();

void pickGame(Games *&stats, int size);

void inputValid(Games *&stats, int size);

void homeTotal(Games *&stats, int size);

void visitTotal(Games *&stats, int size);

void displayData(Games *&stats, int size);

int main() {

//Declare a pointer to the Game structure
    Games *stats = nullptr;

//Read in the data from the games.txt text file
//Use the input valid to make sure none of the values are negative
    int size = readData(stats), num;
    inputValid(stats, size);

//Create a do-while loop that will continue to iterate through the menu
//until the user decides to exit the program
    do {
//call the menu function, which will display the choices to the user
//and store their choice in a variable
        num = menu();
//Break the choices into cases and complete out the functions accordingly
        switch (num) {
            case 1:
                displayData(stats, size);
                break;
            case 2:
                pickGame(stats, size);
                break;
            case 3:
                homeTotal(stats, size);
                break;
            case 4:
                visitTotal(stats, size);
                break;
            case 5:
                break;
            default:
                cout << "Invalid input.\n" << endl;
        }
    } while (num != 5);
    
    return (0);
}

//Dynamically allocate the the structure array and read the information into the array
int readData(Games *&stats) {
    int size;
//Games *stats = nullptr;
    stats = new Games[size];
    stats->visit_team;
    stats->home_score;
    stats->visit_score;

//open a file to read the games.txt file
    ifstream inFile;
    inFile.open("games.txt"); //open the text file salsa
    if (!inFile) {
        cout << "Error opening data file!\n"; // if the file does not open send an error message to the user

        exit(102); //exit the program
    }
    cout << "Reading data from file \n\n";
    inFile >> size;
    inFile.ignore();
    for (int i = 0; i < size; i++) {

        getline(inFile, stats[i].visit_team);
        inFile >> stats[i].home_score;
        inFile >> stats[i].visit_score;
        inFile.ignore();
    }
    inFile.close();
    return (size);
}

void inputValid(Games *&stats, int size) {
    ifstream inFile; //define infile and outfile
    ifstream outFile;
    inFile.open("games.txt"); //open the games.txt file
    for (int i = 0; i < size; i++) {
        if (stats[i].home_score < 0) //Test the value for negative values
        {
            cout << "Negative values are invalid and will be set to zero.\n\n";
            stats[i].home_score = 0; //Set the negative values to 0
            outFile >> stats[i].home_score; //write over the negative value as 0 in the games.txt file
        }
        if (stats[i].visit_score < 0) {
            cout << "Negative values are invalid and will be set to zero.\n\n";
            stats[i].visit_score = 0; //Set the negative values to 0
            outFile >> stats[i].visit_score;
        }
    }
//Close the file
    inFile.close();

}

//Display the menu driven interface for the users to choice which functions they would
//like the program to carry out
int menu() {
//initialize the variable num to zero
    int num = 0;

    cout << "1: Print the information for all the games, in table form." << endl;
    cout << "2: Print the information for a specific game." << endl;
    cout << "3: Print the total points scored during the season for the home team." << endl;
    cout << "4: Print the total points scored against the home team during the season." << endl;
    cout << "5: Exit the program." << endl;
    cout << "Please enter your choice:" << endl;
    cin >> num;
//return the value of num to the main function
    return (num);

}

//this function will display the statistics for a particular
//game based on the team name the user inputs
void pickGame(Games *&stats, int size) {
    string name;
//Get the search string from the user
//cin.ignore();
    cout << "Enter a specific team name(in caps) for their statistics: ";
    cin.ignore();
    getline(cin, name);

    int location;

// loop through the array of structures
// for each structure the plan is to use .find on the visit_team field
    for (int i = 0; i < size; i++) {
        location = stats[i].visit_team.find(name, 0); //figure out what it returns when it has found the location
        if (location >= 0) {
            cout << "Team: " << stats[i].visit_team << endl << "Home score: " << stats[i].home_score << endl
                 << "Visit score: " << stats[i].visit_score << endl;
        }
    }
}

//this function will tally the total number of points the home team has scored
//during the season and display the total
void homeTotal(Games *&stats, int size) {
    int count = 0;
//Calculate the total points scored during the season for the
//home team.
    for (int i = 0; i < size; i++) {
        count += stats[i].home_score;
    }
    cout << "The total points scored during the season for the home team was: " << count << endl;
}

//This function will tally the total point scored against the home team for the
//duration of the season
void visitTotal(Games *&stats, int size) {
    int count = 0;
//Calculate the total points scored during the season for the
//home team.
    for (int i = 0; i < size; i++) {
        count += stats[i].visit_score;
    }
    cout << "The total points scored against the home team during the season was: " << count << endl;
}

//This function will display all the data in a table form for the user
void displayData(Games *&stats, int size) {
    cout << fixed;
    cout << setw(17) << left << "Team name: " << setw(26) << right << " Home score: " << setw(10) << right
         << "Visiting score:" << endl;
    for (int i = 0; i < size; i++) {
        cout << setw(17) << left << stats[i].visit_team << setw(18) << right << stats[i].home_score << setw(18)
             << stats[i].visit_score << endl;
    }
}
Add a comment
Know the answer?
Add Answer to:
I am getting an error that the variable num was not declared in the scope on...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • I need to update this C++ code according to these instructions. The team name should be...

    I need to update this C++ code according to these instructions. The team name should be "Scooterbacks". I appreciate any help! Here is my code: #include <iostream> #include <string> #include <fstream> using namespace std; void menu(); int loadFile(string file,string names[],int jNo[],string pos[],int scores[]); string lowestScorer(string names[],int scores[],int size); string highestScorer(string names[],int scores[],int size); void searchByName(string names[],int jNo[],string pos[],int scores[],int size); int totalPoints(int scores[],int size); void sortByName(string names[],int jNo[],string pos[],int scores[],int size); void displayToScreen(string names[],int jNo[],string pos[],int scores[],int size); void writeToFile(string...

  • The following is a sample inventory in C++, i want to ask the user to input a item number for removing from inventory. //CPP #include <iostream> #include <fstream> #include <cstdlib>...

    The following is a sample inventory in C++, i want to ask the user to input a item number for removing from inventory. //CPP #include <iostream> #include <fstream> #include <cstdlib> #include <iomanip> #define MAX 1000 using namespace std; //Function to Add a new inventory item to the data into the array in memory void addItem(string desc[],string idNum[], float prices[], int qty[],int &num) { cout<<"Enter the names:"; cin>>desc[num]; cout<<"Enter the item number:"; cin>>idNum[num]; cout<<"Enter the price of item:"; cin>>prices[num]; cout<<"Enter the...

  • C++ getline errors I am getting getline is undefined error messages. I would like the variables...

    C++ getline errors I am getting getline is undefined error messages. I would like the variables to remain as strings. Below is my code. #include <iostream> #include<string.h> using namespace std; int index = 0; // variable to hold how many customers are entered struct Address //Structure for the address. { int street; int city; int state; int zipcode; }; // Customer structure struct Customer { string firstNm, lastNm; Address busAddr, homeAddr; }; // Functions int displayMenu(); Customer getCustomer(); void showCustomer(Customer);...

  • fully comments for my program, thank you will thumb up #include <iostream> #include <fstream> #include <string>...

    fully comments for my program, thank you will thumb up #include <iostream> #include <fstream> #include <string> #include <iomanip> using namespace std; struct book { int ISBN; string Author; string Title; string publisher; int Quantity; double price; }; void choice1(book books[], int& size, int MAX_SIZE) { ifstream inFile; inFile.open("inventory.txt"); if (inFile.fail()) cout <<"file could not open"<<endl; string str;    while(inFile && size < MAX_SIZE) { getline(inFile, str); books[size].ISBN = atoi(str.c_str()); getline(inFile, books[size].Title);    getline(inFile, books[size].Author); getline(inFile, books[size].publisher);          getline(inFile,...

  • #include <iostream> #include <fstream> using namespace std; //constants const int CAP = 100; //function prototypes bool...

    #include <iostream> #include <fstream> using namespace std; //constants const int CAP = 100; //function prototypes bool openFile(ifstream &); void readData(ifstream &, int [], int &); void printData(const int [], int); void sum(const int[], int); void removeItem(int[], int &, int); int main() { ifstream inFile; int list[CAP], size = 0; if (!openFile(inFile)) { cout << "Program terminating!! File not found!" << endl; return -1; } //read the data from the file readData(inFile, list, size); inFile.close(); cout << "Data in file:" <<...

  •    moviestruct.cpp #include <iostream> #include <fstream> #include <cstdlib> #include <ostream> #include <fstream> #include <cstdlib> #include <cstring>...

       moviestruct.cpp #include <iostream> #include <fstream> #include <cstdlib> #include <ostream> #include <fstream> #include <cstdlib> #include <cstring> using namespace std; typedef struct{ int id; char title[250]; int year; char rating[6]; int totalCopies; int rentedCopies; }movie; int loadData(ifstream &infile, movie movies[]); void printAll(movie movies[], int count); void printRated(movie movies[], int count); void printTitled(movie movies[], int count); void addMovie(movie movies[],int &count); void returnMovie(movie movies[],int count); void rentMovie(movie movies[],int count); void saveToFile(movie movies[], int count, char *filename); void printMovie(movie &m); int find(movie movies[], int...

  • //This program is your final exam. //Please fill in the functions at the bottom of the...

    //This program is your final exam. //Please fill in the functions at the bottom of the file. (evenCount and insertItem) //DO NOT CHANGE ANYTHING ELSE. //main has all the code needed to test your functions. Once your functions are written, please build and make sure it works fine //Note that in this case, the list is not sorted and does not need to be. Your goal is to insert the number in the given position. #include <iostream> #include <fstream> using...

  • //This program is your final exam. //Please fill in the functions at the bottom of the...

    //This program is your final exam. //Please fill in the functions at the bottom of the file. (evenCount and insertItem) //DO NOT CHANGE ANYTHING ELSE. //main has all the code needed to test your functions. Once your functions are written, please build and make sure it works fine //Note that in this case, the list is not sorted and does not need to be. Your goal is to insert the number in the given position. #include <iostream> #include <fstream> using...

  • Am I getting this error because i declared 'n' as an int, and then asking it...

    Am I getting this error because i declared 'n' as an int, and then asking it to make it a double? This is the coude: #include "stdafx.h" #include <iostream> #include <fstream> #include <string> #include <algorithm> #include <vector> using namespace std; void sort(double grades[], int size); char calGrade(double); int main() {    int n;    double avg, sum = 0;;    string in_file, out_file;    cout << "Please enter the name of the input file: ";    cin >> in_file;   ...

  • I want to change this code and need help. I want the code to not use...

    I want to change this code and need help. I want the code to not use parallel arrays, but instead use one array of struct containing the data elements, String for first name, String for last name,Array of integers for five (5) test scores, Character for grade. If you have any idea help would be great. #include #include #include #include using namespace std; const int NUMBER_OF_ROWS = 10; //number of students const int NUMBER_OF_COLUMNS = 5; //number of scores void...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT