I have no idea how i would reject invalid inputs for the exams and assignments (valid numbers are 0.00 - 100.00). but i also need to give the user one more chance to enter a valid input. so if they put in two invalid inputs in a row for the same variable it tells the user that they need to restart the program and prevent it from running. Im new to programming so im not very good at this.
#include <iostream>
#include <string>
#include <iomanip>
using namespace std;
int main ()
{
float exam_1;
float exam_2;
float exam_3;
float assignment_1;
float assignment_2;
float weighted_exam = .1667;
float weighted_assignment = .25;
float min_score = 0.00;
float max_score = 100.00;
string name;
cout << "Please enter student name <First Last>: "; // this will ask for the students first and last name
getline(cin, name);
cout << "\n";
cout << "\t Be sure to include the decimal point for scores.\n";
cout <<"\t !!! All scores should range from 0.00 to 100.00!!! \n";
cout << "\t For example: 80.50 \n";
cout << "\n";
cout << "Please enter your exam 1 score: ";
cin >> exam_1;
cout << "Please enter your exam 2 score: ";
cin >> exam_2;
cout << "Please enter your exam 3 score: ";
cin >> exam_3;
cout << "Please enter your assignment 1 score: ";
cin >> assignment_1;
cout << "Please enter your assignment 2 score: ";
cin >> assignment_2;
cout << endl;
cout << "-" << "OUTPUT" << "-\n";
return 0;
}
You can do like this:
do{
cout <<"\t !!! All scores should range from 0.00 to 100.00!!! \n";
cin >> exam_1;
while(exam_1 < 0.0 || exam_1>100.0);
Repeat this to all inputs
cout << "Please enter your exam 1 score: ";
cin >> exam_1;
cout << "Please enter your exam 2 score: ";
cin >> exam_2;
cout << "Please enter your exam 3 score: ";
cin >> exam_3;
To
auto ReadExamScore = [](auto& ex, char c)
{
cout << "Please enter your exam " << c << " score: ";
for (int i = 0; i < 2; i ++)
{
if (cin >> ex)
{
if (ex>= 0.0 && ex <= 100.00)
return true;
}
cout << "Please enter a valid exam score" << endl;
}
cout << "Press any key to exit..." << endl;
getchar();
exit(0);
};
ReadExamScore(exam_1,'1');
ReadExamScore(exam_2,'2');
ReadExamScore(exam_3,'3');
Related
So I am trying to create a ATM system that lets user to input value such as Account number, account name and amount. But I can't figure out what exactly I have to do
int AccNum[2];
string AccName[2];
float AccBal[2];
cout << "********** ENTER ACCOUNT **********"<<endl;
for(int num = 0; num < 2; num++){
cout << "Enter Account number: ";
cin >> AccNum[num];
for(int name = num; name < 2; name++){
cout << "Enter Account Name: ";
getline(cin, AccName[name]);
for(int bal = name; bal < 2; bal++){
cout << "Enter Amount: ";
cin >> AccBal[bal];
}
}
}
I have tried something like this but it does not give the result that I want. The ideal result would be
********** ENTER ACCOUNT **********
Enter Account number: 1231232
Enter Account name: James white
Enter amount: 1000
it will run 2 times so there would be 2 accounts after this loop that will have a result like this
********** THE ACCOUNT IS **********
Account number: 1231232
Account name: James white
Balance: 1000
So the code you wrote is nearly good. Too many loops in my opinion
int AccNum[2];
string AccName[2];
float AccBal[2];
cout << "********** ENTER ACCOUNT **********"<<endl;
for(int num = 0; num < 2; num++){ // lets call this loop a. happens 2 times
cout << "Enter Account number: ";
cin >> AccNum[num];
for(int name = num; name < 2; name++){ // loop b happens 2 times * loop a times
cout << "Enter Account Name: ";
getline(cin, AccName[name]);
for(int bal = name; bal < 2; bal++){ loop c = 2 * a * b = 8
cout << "Enter Amount: ";
cin >> AccBal[bal];
}
}
}
Direct fix:
int main()
{
int AccNum[2];
string AccName[2];
float AccBal[2];
cout << "********** ENTER ACCOUNT **********" << endl;
for(int num = 0; num < 2; num++){
cout << "Enter Account number: ";
cin >> AccNum[num];
cout << "Enter Account Name: ";
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
getline(cin, AccName[num]);
cout << "Enter Amount: ";
cin >> AccBal[num];
}
for(int i = 0; i < 2; i++)
cout << "********** ENTER ACCOUNT **********" << endl
<< "Account number: " << AccNum[i] << endl
<< "Account name: " << AccName[i] << endl
<< "Balance: " << AccBal[i] << endl << endl;
}
But if you want to expand it a little bit:
#include <iostream>
#include <vector>
using namespace std;
struct Account
{
int Number;
string Name;
float Balance;
Account(int num, string nam, float bal) : Number(num), Name(nam), Balance(bal) {}
Account() {}
void getData()
{
cout << "Enter Account number: ";
cin >> Number;
cout << "Enter Account Name: ";
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
getline(cin, Name);
cout << "Enter Amount: ";
cin >> Balance;
}
void printAccount()
{
cout << "********** ENTER ACCOUNT **********" << endl
<< "Account number: " << Number << endl
<< "Account name: " << Name << endl
<< "Balance: " << Balance << endl << endl;
}
};
int main()
{
vector<Account> accounts;
for(int i = 0; i < 2; i++)
{
Account person;
person.getData();
accounts.emplace_back(person);
}
for(int i = 0; i < 2; i++) accounts[i].printAccount();
}
Both codes give the exact same output:
When I try to compile, I get only two error messages.
"Error LNK2019 unresolved external symbol "int __cdecl examAvg(void)" (?examAvg##YAHXZ) referenced in function _main
Line 1
and
LNK1120 1 unresolved externals
also on Line 1
I have worked on this so long (days) that I really can't figure out where I am going wrong anymore, I am sure it is something simple, but I am for a loss again.
Will someone please give me a suggestion to help rectify the error codes?
/*
Christopher Pierce
Date Created: July 3, 2020
Egrades Vector Application
*This application creates a C++ menu program that generates an electronic grade sheet that can keep track of five student test scores earned during the semester.
• Organizes the main program menu to call four functions.
• (1) function that allow a user to Add five students and their scores on three exams into the vector.
• (2) function that will compute the average test score for each of the five students in the vector.
• (3) function that all the user to display a student average test scores searching for their name in the vector.
• (4) function that will compute the average of the average exams in the vector and display the result.
*/
#include <iostream>
#include <string>
#include <cstdlib>
#include <iomanip>
#include <vector>
#include <cmath>
#include <algorithm>
using namespace std;
// Declarations
int score1;
int score2;
int score3;
int score4;
int score5;
int score6;
int score7;
int score8;
int score9;
int score10;
int score11;
int score12;
int score13;
int score14;
int score15;
string student1;
string student2;
string student3;
string student4;
string student5;
int main()
{
vector <string> StudentFile;
vector <int> Egrades;
int average1 = (score1 + score2 + score3) / 3;
int average2 = (score4 + score5 + score6) / 3;
int average3 = (score7 + score8 + score9) / 3;
int average4 = (score10 + score11 + score12) / 3;
int average5 = (score13 + score14 + score15) / 3;
// Prototype (functions)
int examAvg();
{
StudentFile.insert(StudentFile.begin(), student1);
Egrades.insert(Egrades.begin(), average1);
StudentFile.push_back(student2);
Egrades.push_back(average2);
StudentFile.push_back(student3);
Egrades.push_back(average3);
StudentFile.push_back(student4);
Egrades.push_back(average4);
StudentFile.push_back(student5);
Egrades.push_back(average5);
}
/*
Start of Menu
*/
// Defines the width of the menu
const int menuWidth = 84;
// This is a loop structure for the menu box using ASCII
// This prints the top left corner of the menu box (ASCII)
cout << char(201);
// This prints the top border of the menu box (ASCII)
for (int column = 0; column < menuWidth; ++column) {
cout << char(205);
}
// This prints the top right corner of the menu box (ASCII)
cout << char(187);
cout << "\n";
// array with menu options
string menu[15];
menu[0] = " **************";
menu[1] = " *** MAIN MENU ***";
menu[2] = " **************";
menu[3] = " Please Choose From The Following Options:";
menu[6] = " 1. Add 5 Students to Egrades Vector:";
menu[8] = " 2. Compute Student Test Score Average:";
menu[10] = " 3. Search A Student Average Test Score:";
menu[12] = " 4. Compute The Average Of All 5 Student Exam Averages:";
menu[14] = " 5. Exit";
for (string option : menu)
{
cout << char(186) // print left border
<< setw(menuWidth) // set next item width
<< left // set next item aligment
<< option // print menu option string with width and aligment
<< char(186) << "\n"; // print right border
}
// This will print the bottom left corner of the menu box (ASCII)
cout << char(200);
// This prints the bottom border of the menu box (ASCII)
for (int column = 0; column < menuWidth; ++column) {
cout << char(205);
}
// This prints the bottom right corner of the menu box (ASCII)
cout << char(188);
cout << "\n\n";
/*
END OF MENU
*/
char selection;
cout << "\t\t\t Enter Your Selection: ", cin >> selection, cout << endl;
switch (selection)
{
case '1':
system("CLS");
cout << "Student Information Section:\n\n";
// Student 1 Info, captures name and scores
cout << "\nEnter Student 1 Name: \n";
cin >> student1;
cout << "\nEnter Exam 1 Score: ";
cin >> score1;
cout << "Enter Exam 2 Score: ";
cin >> score2;
cout << "Enter Exam 3 Score: ";
cin >> score3; examAvg();
// Student 2 Info, captures name and scores
cout << "\nEnter Student 2 Name:\n";
cin >> student2;
cout << "\nEnter Exam 1 Score: ";
cin >> score4;
cout << "Enter Exam 2 Score: ";
cin >> score5;
cout << "Enter Exam 3 Score: ";
cin >> score6; examAvg();
// Student 3 Info, captures name and scores
cout << "\nEnter Student 3 Name:\n";
cin >> student3;
cout << "\nEnter Exam 1 Score: ";
cin >> score7;
cout << "Enter Exam 2 Score: ";
cin >> score8;
cout << "Enter Exam 3 Score: ";
cin >> score9; examAvg();
// Student 4 Info, captures name and scores
cout << "\nEnter Student 4 Name:\n";
cin >> student4;
cout << "\nEnter Exam 1 Score: ";
cin >> score10;
cout << "Enter Exam 2 Score: ";
cin >> score11;
cout << "Enter Exam 3 Score: ";
cin >> score12; examAvg();
// Student 5 Info, captures name and scores
cout << "\nEnter Student 5 Name:\n";
cin >> student5;
cout << "\nEnter Exam 1 Score: ";
cin >> score13;
cout << "Enter Exam 2 Score: ";
cin >> score14;
cout << "Enter Exam 3 Score: ";
cin >> score15; examAvg();
cout << "\n\n******************************************************\n";
cout << "!!! INFORMATION HAS BEEN ADDED TO EGRADES VECTOR !!!\n";
cout << "******************************************************\n\n";
cout << "To Return To Main Menu:\n\n";
system("pause");
return main();
break;
case '2':
system("CLS");
for (unsigned int i = 0; i < Egrades.size(); ++i)
{
cout << Egrades[i] << endl;
}
cout << "To Return To Main Menu:\n\n";
system("pause");
return main();
break;
case '3':
system("CLS");
int score;
cout << "\nEnter A Students Name To Find Their Average:\n";
cin >> score;
if (cin >> student1)
{
cout << "Average Found:\n" << average1;
}
else if (cin >> student2)
{
cout << "Average Found:\n" << average2;
}
else if (cin >> student3)
{
cout << "Average Found:\n" << average3;
}
else if (cin >> student4)
{
cout << "Average Found:\n" << average4;
}
else if (cin >> student5)
{
cout << "Average Found:\n" << average5;
}
else
{
cout << "Average not found.\n";
}
cout << "To Return To Main Menu:\n\n";
system("pause");
return main();
break;
case '4':
system("CLS");
cout << "The Average of All Five Students Averages Are:\n\n";
cout << "|| " << (average1 + average2 + average3 + average4 + average5) / 5 << " ||\n\n";
cout << "To Return To Main Menu:\n\n";
system("pause");
return main();
break;
case '5':
exit(-1);
break;
default: cout << "\n Invalid selection\n\n";
}
return 0;
}
My issue is that I have set up an array to store totals that were calculated from values read from a file. These stored totals are then added together to find the over all average.
This issue is stemming from a 'cin' at the beginning of the program where the user inputs a number and that number is supposed to drive the program by setting how many times the program loops and how many modules are inside the array. The array does not seem to work properly no matter how much I try.
#include <iostream>
#include <iomanip>
#include <string>
#include <fstream>
using namespace std;
int main()
{
string StudentGrades;
int studentID;
double quiz1;
double quiz2;
double quiz3;
double quiz4;
int total = 0;
double choice;
ofstream outFile;
double numStud=1;
cout << "Enter student ID number, Quiz 1 Grade, Quiz 2 Grade , Quiz 3 Grade, Quiz 4 Grade" << endl;
outFile.open("StudentGrades.txt");
cout << "How many students would you like to enter?" << endl;
cin >> numStud;
for (int x = 0; x < numStud; x++)
{
cout << "Enter student ID: ";
cin >> studentID;
cout << "Enter quiz grade 1: ";
cin >> quiz1;
//cout << quiz1;
cout << "Enter quiz grade 2: ";
cin >> quiz2;
//cout << quiz2;
cout << "Enter quiz grade 3: ";
cin >> quiz3;
//cout << quiz3;
cout << "Enter quiz grade 4: ";
cin >> quiz4;
//cout << quiz4;
cout << endl;
//outFile.open("StudentGrades.txt");
if (outFile.is_open())
{
cout << "inside if/else outFile" << endl;
//outFile << "File successfully open";
outFile << studentID << " " << quiz1 << " " << quiz2 << " " << quiz3 << " " << quiz4 << endl;
}
else
{
cout << "Error opening file";
}
outFile.close();
/*cout << "Enter 0 for no more students. Enter 1 for more students." << endl;
cin >> choice;
if (choice == 1)
continue;
if (choice == 0)
{
outFile.close();
break;
}*/
}
ifstream inFile;
inFile.open("StudentGrades.txt");
int sTotal;
int total[numStud];
while (inFile >> studentID >> quiz1 >> quiz2 >> quiz3 >> quiz4)
{
//cout << studentID << " " << quiz1 << " " << quiz2 << " " << quiz3 << " " << quiz4 << endl;
total = (quiz1 + quiz2 + quiz3 + quiz4);
sTotal = total[numStud];
double avg = total / 4;
}
system("pause");
return 0;
}
int total[numStud]; is a variable length array and is not standard in C++. If you need an array and you don't know what the size will be then you should use a std::vector. A vector can be used almost exactly as an array can. For example you could would become:
int total;
std::vector<int> studentTotal;
while (inFile >> studentID >> quiz1 >> quiz2 >> quiz3 >> quiz4)
{
//cout << studentID << " " << quiz1 << " " << quiz2 << " " << quiz3 << " " << quiz4 << endl;
studentTotal.push_back(quiz1 + quiz2 + quiz3 + quiz4); // insert into the vector at the end
total += studentTotal.back(); // get last inserted element
}
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
I was wondering if someone could help me with my code. I am not sure how to call specific values from a text file that had values put into it by the user.
the text file would look like this
1000 90 80 50 60
1001 60 70 100 90
1002 100 30 50 70
I need to add each of the numbers after the 4-digit number and then divide them.
I want to be able to do this through a nested loop.
#include <iostream>
#include <iomanip>
#include <string>
#include <fstream>
using namespace std;
int main()
{
string StudentGrades;
int studentID;
double quiz1;
double quiz3;
double quiz4;
double total = 0;
double choice;
ofstream outFile;
cout << "Enter student ID number, Quiz 1 Grade, Quiz 2 Grade , Quiz 3 Grade, Quiz 4 Grade" << endl;
outFile.open("StudentGrades.txt");
//while (outFile.open)
//{
for (int x = 0; x < 4; x++)
{
cout << "Enter student ID: ";
cin >> studentID;
cout << "Enter quiz grade 1: ";
cin >> quiz1;
//cout << quiz1;
cout << "Enter quiz grade 2: ";
cin >> quiz2;
//cout << quiz2;
cout << "Enter quiz grade 3: ";
cin >> quiz3;
//cout << quiz3;
cout << "Enter quiz grade 4: ";
cin >> quiz4;
//cout << quiz4;
cout << endl;
//outFile.open("StudentGrades.txt");
if (outFile.is_open())
{
cout << "inside if/else outFile" << endl;
//outFile << "File successfully open";
outFile << studentID << " " << quiz1 << " " << quiz2 << " " << quiz3 << " " << quiz4 << endl;
}
else
{
cout << "Error opening file";
}
cout << "Enter 0 for no more students. Enter 1 for more students." << endl;
cin >> choice;
if (choice == 1)
continue;
if (choice == 0)
{
outFile.close();
break;
}
}
//}
//declaring file and opening it
ifstream inFile;
inFile.open("StudentGrades.txt");
while (inFile >> studentID<<)
{
cout << studentID << quiz1 << quiz2 << quiz3 << quiz4 << endl;
}
system("pause");
return (0);
}
This would be the idiomatic loop for reading a file of that format:
while (inFile >> studentID >> quiz1 >> quiz2 >> quiz3 >> quiz4)
{
// Do some arithmetic
}
I have no idea what good a nested loop would do.
this should work
#include <iostream>
#include <iomanip>
#include <string>
#include <fstream>
using namespace std;
int main()
{
string StudentGrades;
int studentID;
double quiz1;
double quiz2;
double quiz3;
double quiz4;
double total = 0;
double choice;
ofstream outFile;
cout << "Enter student ID number, Quiz 1 Grade, Quiz 2 Grade , Quiz 3 Grade, Quiz 4 Grade" << endl;
outFile.open("StudentGrades.txt");
for (int x = 0; x < 4; x++)
{
cout << "Enter student ID: ";
cin >> studentID;
cout << "Enter quiz grade 1: ";
cin >> quiz1;
//cout << quiz1;
cout << "Enter quiz grade 2: ";
cin >> quiz2;
//cout << quiz2;
cout << "Enter quiz grade 3: ";
cin >> quiz3;
//cout << quiz3;
cout << "Enter quiz grade 4: ";
cin >> quiz4;
//cout << quiz4;
cout << endl;
//outFile.open("StudentGrades.txt");
if (outFile.is_open())
{
cout << "inside if/else outFile" << endl;
//outFile << "File successfully open";
outFile << studentID << " " << quiz1 << " " << quiz2 << " " << quiz3 << " " << quiz4 << endl;
}
else
{
cout << "Error opening file";
}
cout << "Enter 0 for no more students. Enter 1 for more students." << endl;
cin >> choice;
if (choice == 1)
continue;
if (choice == 0)
{
outFile.close();
break;
}
}
ifstream inFile;
inFile.open("StudentGrades.txt");
while (inFile >> studentID >> quiz1 >> quiz2 >> quiz3 >> quiz4)
{
cout << studentID << " " << quiz1 << " " << quiz2 << " " << quiz3 << " " << quiz4 << endl;
}
return 0;
}
you need to read the scores in that file and store in every designated variable
while (inFile >> studentID >> quiz1 >> quiz2 >> quiz3 >> quiz4)
{
cout << studentID << " " << quiz1 << " " << quiz2 << " " << quiz3 << " " << quiz4 << endl;
}
The problem I am having is that I do not know how to read a specific number out of a text file that was created and had values entered into it earlier in the program.
I was hoping to use a nested loop statement for reading in the values after the values of the quizes are read in I need to add up the values and average them out and this needs to happen multiple times
#include <iostream>
#include <iomanip>
#include <string>
#include <fstream>
using namespace std;
int main()
{
string StudentGrades;
int studentID;
double quiz1;
double quiz2;
double quiz3;
double quiz4;
double total=0;
double choice;
ofstream outFile;
cout << "Enter student ID number, Quiz 1 Grade, Quiz 2 Grade , Quiz 3 Grade, Quiz 4 Grade" << endl;
outFile.open("StudentGrades.txt");
//while (outFile.open)
//{
for (int x = 0; x < 4; x++)
{
cout << "Enter student ID: ";
cin >> studentID;
cout << "Enter quiz grade 1: ";
cin >> quiz1;
//cout << quiz1;
cout << "Enter quiz grade 2: ";
cin >> quiz2;
//cout << quiz2;
cout << "Enter quiz grade 3: ";
cin >> quiz3;
//cout << quiz3;
cout << "Enter quiz grade 4: ";
cin >> quiz4;
//cout << quiz4;
cout << endl;
//outFile.open("StudentGrades.txt");
if (outFile.is_open())
{
cout << "inside if/else outFile" << endl;
//outFile << "File successfully open";
outFile << studentID << " " << quiz1 << " " << quiz2 << " " << quiz3 << " " << quiz4 << endl;
}
else
{
cout << "Error opening file";
}
cout << "Enter 0 for no more students. Enter 1 for more students." << endl;
cin >> choice;
if (choice == 1)
continue;
if (choice == 0)
{
outFile.close();
break;
}
}
//}
//declaring file and opening it
ifstream inFile;
inFile.open("StudentGrades.txt");
while (inFile>>studentID)
{
cout << studentID<< quiz1 <<quiz2<<quiz3<<quiz4<< endl;
}
system("pause");
return (0);
}
You are not reading all the fields from the input file, you are reading just the studentID.
while (inFile>>studentID)
{
cout << studentID<< quiz1 <<quiz2<<quiz3<<quiz4<< endl;
}
Try:
while (inFile >> studentID >> quiz1 >> quiz2 >> quiz3 >> quiz4)
{
cout << studentID << quiz1 << quiz2 << quiz3 << quiz4 << endl;
}
Update
Below is my suggestion for a refactored program. It's good to create functions that perform specific tasks and then call them from a higher level function instead of jamming them all in one large function.
#include <iostream>
#include <iomanip>
#include <string>
#include <fstream>
using namespace std;
void saveStudentData(string const& filename)
{
int studentID;
double quiz1;
double quiz2;
double quiz3;
double quiz4;
// Use int type for choice, not double
int choice;
ofstream outFile(filename);
if (!outFile.is_open())
{
// Problem opening file.
cout << "Error opening file";
return;
}
cout << "Enter student ID number, Quiz 1 Grade, Quiz 2 Grade , Quiz 3 Grade, Quiz 4 Grade" << endl;
for (int x = 0; x < 4; x++)
{
cout << "Enter student ID: ";
cin >> studentID;
cout << "Enter quiz grade 1: ";
cin >> quiz1;
cout << "Enter quiz grade 2: ";
cin >> quiz2;
cout << "Enter quiz grade 3: ";
cin >> quiz3;
cout << "Enter quiz grade 4: ";
cin >> quiz4;
cout << endl;
outFile << studentID << " " << quiz1 << " " << quiz2 << " " << quiz3 << " " << quiz4 << endl;
cout << "Enter 0 for no more students. Enter 1 for more students." << endl;
cin >> choice;
if (choice == 0)
{
break;
}
}
}
void readStudentData(string const& filename)
{
int studentID;
double quiz1;
double quiz2;
double quiz3;
double quiz4;
double total=0;
ifstream inFile(filename);
while (inFile >> studentID >> quiz1 >> quiz2 >> quiz3 >> quiz4)
{
total = (quiz1 + quiz2 + quiz3 + quiz4);
cout << studentID << " " << quiz1 << " " << quiz2
<< " " << quiz3 << " " << quiz4 << " " << total << endl;
}
}
int main()
{
string filename("StudentGrades.txt");
saveStudentData(filename);
readStudentData(filename);
return (0);
}