it's required to design a system that takes all the data relevant to the student such as, name,ID,no.of subjects ,and their marks. it's required that when i search for a specific student to show all the data about him and to calculate his gpa and print the result. i tried but the output wasn't as expected and here's my code:
//how the data is read in the txt file`
void enter()
{
ofstream file1("cairo.txt", ios::app);
cout << "Enter the student's name : ";
getline(cin, name);
file1 << name << "\t";
cout << "Enter the student's ID : ";
cin >> std_num;
file1 << std_num << "\t";
cout << "Enter your department : ";
cin.ignore();
getline(cin, department);
file1 << department << "\t";
cout << "Enter the student's number for subjects : ";
cin >> num_sub;
file1 << num_sub << "\t";
for (int i = 0; i < num_sub; i++) {
cout << "please enter the student's " << i + 1 << " " << "mark \n";
cin >> grades[i];
file1 << grades[i] << "\t";
}
file1 << "\n";
file1.close();}
void get_grades()
{
char name[30], id[10], dep[25], city[15], resd[15], zip[15];
int nsub,nmarks[10];
float sum = 0;
ifstream gfile("students.txt");
char rq[30];
cout << "Enter the name of the required student to get his grades : ";
cin.getline(rq, 25);
while (!gfile.eof())
{
if (strcmp(name, rq) == 0)
{
gfile.getline(name, 30, '|');
gfile.getline(id, 10, '|');
gfile.getline(dep, 25, '|');
gfile >> num_sub;
for (int i=0; i < nsub; i++)
{
gfile1>>nmarks[i];
sum += nmarks[i];
}
psum = (sum/(nsub*100));
if (psum < 0.5)) cout<<" Your Grade is F "<<endl;
else if (psum >= 0.5 && psum < 0.65) cout<<" Your Grade is D "<<endl;
else if (psum >= 0.65 && psum < 0.75) cout<<" Your Grade is C "<<endl;
else if (psum >= 0.75 && psum < 0.85) cout<<" Your Grade is B "<<endl;
else cout<<" Your Grade is A "<<endl;
}
}
}
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:
Here is the link to the assignment
I managed to get the majority of the project working except the void finalSales() function. I'm not too sure how to go about it with a one and two-dimensional array and display it accordingly. I have a midterm exam coming up next week and this chapter is included. I added some comments on what the functions do below:
#include <iostream>
#include <cstdlib>
#include <string>
using namespace std;
bool isPlaying = true, isPlaying_Two = true, isPlaying_Three = true, isLooping = true;
string salesPerson[6] = { "Jericho Rosales", "lisa Soberano", "Kim Chiu", "maja Salvador", "Katheryn Bern", "Daniel Padilla" },
items[3] = { "Washing Machine", "Refrigerator", "Music System" };
double table[6][3], totalSales[6] = {0};
int salesNum, productNum;
double saleAmount;
void Sales();
void person();
void prod();
void finalSales();
int main()
{
while (isLooping) {
Sales();
}
}
//main game
void Sales() {
while (isPlaying)
{
person();
if (salesNum > 0 && salesNum < 7) {
//cout << "debug " << salesPerson[salesNum - 1];//debug
cout << "\n";
while (isPlaying_Two)
{
prod();
if (productNum > 0 && productNum < 4) {
//cout << "debug " << items[productNum - 1];//debug
while (isPlaying_Three)
{
finalSales();
}
}
else
{
cout << "\n";
cout << "Input out of range, please try again\n";
cout << "\n";
}
}
}
else {
cout << "\n";
cout << "Input out of range, please try again\n";
cout << "\n";
}
}
}
//user selects which salesperson
void person() {
cout << "Sales Registry\n";
for (int i = 0; i < 6; i++)
{
cout << i + 1 << ". " << salesPerson[i] << "\n";
}
cout << "Select the Salesperson by typing his ordinal number: ";
cin >> salesNum;
//check if cin failed
while (cin.fail()) {
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), '\n');
cout << "Invalid entry, enter again ";
cin >> salesNum;
}
}
//user selects which product
void prod() {
for (int i = 0; i < 3; i++)
{
cout << i + 1 << ". " << items[i] << "\n";
}
cout << "Select the product by typing the product ordinal number: ";
cin >> productNum;
//check if cin failed
while (cin.fail()) {
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), '\n');
cout << "Invalid entry, enter again ";
cin >> productNum;
}
}
//get the sales amount(enter a random number in) and
//if the user wants to enter more sales it goes back to the beginning and asks again while remembering the previous values
//if not then the program adds up the amount (if there is more than one) and displays it accordingly to the salesperson and the product
void finalSales() {
string again;
cout << "Enter the sales amount: ";
cin >> saleAmount;
//check if cin failed
while (cin.fail()) {
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), '\n');
cout << "Invalid entry, enter again ";
cin >> saleAmount;
}
cout << "Do you want to enter more sales? Type y for yes and n for no: ";
cin >> again;
//check if cin failed
while (cin.fail()) {
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), '\n');
cout << "Invalid entry, enter again ";
cin >> saleAmount;
}
if (again == "y" || again == "Y") {
Sales();
}
else {
cout << "\n";
cout << "Salesperson" << "\t\t" << "Washing Machine" << "\t\t" << "Refrigerator" << "\t\t" << "Music system\n";
cout << "****************************************************************************************************";
cout << "\n";
for (int x = 0; x < 6; x++)
{
cout << salesPerson[x];
for (int y = 0; y < 3 ; y++)
{
cout << "\t\t\t" << table[x][y];
}
cout << "\n";
}
}
}
You need to return index from person() and prod() then pass them to finalSales. Also pass array to it and increase the amount of product by the saleAmount. And don't forget to make your bool variables false or you end up with infinite loop. Create two int variables in Sales(). Initialize them by returning the index from person() and the other one from product().
void finalSales(int idx1, int idx2,double table[6][3]) {
string again;
cout << "Enter the sales amount: ";
cin >> saleAmount;
table[idx1][ idx2] = saleAmount;
//check if cin failed
while (cin.fail()) {
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), '\n');
cout << "Invalid entry, enter again ";
cin >> saleAmount;
}
cout << "Do you want to enter more sales? Type y for yes and n for no: ";
cin >> again;
//check if cin failed
while (cin.fail()) {
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), '\n');
cout << "Invalid entry, enter again ";
cin >> saleAmount;
}
if (again == "y" || again == "Y") {
Sales();
}
else {
::isPlaying_Three = false;
::isPlaying_Two = false;
::isPlaying = false;
::isLooping = false;
cout << "\n";
cout << "Salesperson" << "\t\t" << "Washing Machine" << "\t\t" << "Refrigerator" << "\t\t" << "Music system\n";
cout << "****************************************************************************************************";
cout << "\n";
for (int x = 0; x < 6; x++)
{
cout << salesPerson[x];
for (int y = 0; y < 3; y++)
{
cout << "\t\t\t" << table[x][y];
}
cout << "\n";
}
}
}
That's for person() and the same for prod().
int person() {
cout << "Sales Registry\n";
for (int i = 0; i < 6; i++)
{
cout << i + 1 << ". " << salesPerson[i] << "\n";
}
cout << "Select the Salesperson by typing his ordinal number: ";
cin >> salesNum;
//check if cin failed
while (cin.fail()) {
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), '\n');
cout << "Invalid entry, enter again ";
cin >> salesNum;
}
return salesNum-1;
}
Don't forget to decrease index by 1.
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 5 years ago.
Improve this question
the program works fine and executes all options, but in the 3rd and 4th option, in 3rd when i search for a student the program doesn't go beyond the data input part while in 4th when i search for data and print it, it gives garbage values in the variables with int datatype while doesn't display string or char type variables. any help would be much appreciated. EDIT: sorry for the inconvenience, I've now separated the functions form the main code. Hope its its more understandable now.
#include<iostream>
#include<string>
using namespace std;
int ncr;
int me,n=0,u,y,l;
char opt1,opt2,opt3,opt4;
struct student
{
string Name;
int DoB;
int Age;
string Address;
string Course[5];
char grade[5];
};
void add();
void remove();
void update();
void search();
int main()
{ int opt;
student s[n];
while(1)
{
cout<<" select what you want to do \n\n 1. Add new record \n2. Remove record\n3. Update Record\n4. Search for particular student\n" ;
cin>>opt;
if(opt==1)
{add();}
else if(opt==2)
{remove();}
else if(opt==3)
{update();}
else if(opt==4)
{search();}
}
}
.
void add(){
n++;
student s[n];
do
{ cout<<"enter name of student ";
cin>>s[n-1].Name;
cout<<"enter DoB of student ";
cin>>s[n-1].DoB;
cout<<"enter age of student ";
cin>>s[n-1].Age;
cout<<"enter address of student ";
cin>>s[n-1].Address;
cout<<"enter courses and grades (max 5) \n";
for(int x=0;x<5;x++){
cout<<"course:";
cin>>s[n-1].Course[x];
cout<<"grade:";
cin>> s[n-1].grade[x];}
cout<<"Roll No. : "<<n<<"\nName :"<<s[n-1].Name<<"\nDoB :"<<s[n-1].DoB<<"\nAge :"<<s[n-1].Age<<"\nAddress :"<<s[n-1].Address;
for(int x=0;x<5;x++){
cout<<"\n"<<s[n-1].Course[x];
cout<<" grade : "<< s[n-1].grade[x]<<"\n";}
cout<<"repeat for another student 'y' or 'n' ?";
cin>>opt1;
}
while(opt1=='y'|| opt1=='Y');
}
.
void remove(){student s[n];
do
{
cout<<"enter student roll no to remove data";cin>>l;
for(int i=l;i<n;i++)
{ s[l-1].Name=s[l].Name;
s[l-1].DoB= s[l].DoB;
s[l-1].Age=s[l].Age;
s[l-1].Address=s[l].Address;
for(int j=0;j<5;j++){
s[l-1].Course[j]=s[l].Course[j];
s[l-1].grade[j]=s[l].grade[j];}
}
cout<<"Record Removed\n";
n--;
cout<<"repeat ? 'y' or 'n' ";
cin>>opt2;
}
while(opt2 == 'y' || opt2 == 'Y');}
.
void update(){
student s[n];
do
{
cout<<"enter the roll no of student you want to update data";
cin>>u;
cout<<"Roll No. : "<<u;
cout<<"\nName : ";
cout<<s[u-1].Name;
cout<<"\nDoB : ";
cout<<s[u-1].DoB;
cout<<"\nAge : ";
cout<<s[u-1].Age;
cout<<"\nAddress :";
cout<<s[u-1].Address;
cout<<"\nCourses and Grades\n";
for(int r=0;r<5;r++)
{
cout<<s[u-1].Course[r];
cout<<"\t"<< s[u-1].grade[r]<<endl;
}
cout<<"enter name of student ";
cin>>s[u-1].Name;
cout<<"enter DoB of student ";
cin>>s[u-1].DoB;
cout<<"enter age of student ";
cin>>s[u-1].Age;
cout<<"enter address of student ";
cin>>s[u-1].Address;
cout<<"enter courses and grades (max 5) \n";
for(int x=0;x<5;x++){
cout<<"course:";
cin>>s[u-1].Course[x];
cout<<"grade:";
cin>> s[u-1].grade[x];}
cout<<"Roll No. : "<<u;
cout<<"\nName : ";
cout<<s[u-1].Name;
cout<<"\nDoB : ";
cout<<s[u-1].DoB;
cout<<"\nAge : ";
cout<<s[u-1].Age;
cout<<"\nAddress :";
cout<<s[u-1].Address;
cout<<"\nCourses and Grades\n";
for(int r=0;r<5;r++)
{ cout<<s[u-1].Course[r];
cout<<"\t"<< s[u-1].grade[r]<<endl; }
cout<<"repeat ? 'y' or 'n' ";
cin>>opt3;
}
while(opt3 == 'y' || opt3 == 'Y');
}
.
void search(){
student s[n];
do
{
cout<<"enter the roll no of student you want to search data of";
cin>>y;
cout<<"Roll No. : "<<n<<"\nName :"<<s[y-1].Name<<"\nDoB :"<<s[y-1].DoB<<"\nAge :"<<s[y-1].Age<<"\nAddress :"<<s[y-1].Address<<"\nCourses and Grades\n";
for(int r=0;r<5;r++)
{
cout<<s[y-1].Course[r];
cout<<"\t"<< s[y-1].grade[r]<<endl;
}
cout<<"repeat ? 'y' or 'n' ";
cin>>opt4;
}
while(opt4 == 'y' || opt4 == 'Y');}
This should look a bit more like a code intended for humans to read :)
#include<iostream>
#include<string>
using namespace std;
int ncr;
int me,n=0,u,y,l;
char opt1,opt2,opt3,opt4;
struct student
{
string Name;
int DoB;
int Age;
string Address;
string Course[5];
char grade[5];
};
void add();
void remove();
void update();
void search();
int main()
{
int opt;
student s[n];
while(1)
{
cout << " select what you want to do \n\n 1. Add new record \n2. Remove record\n3. Update Record\n4. Search for particular student\n" ;
cin >> opt;
if(opt==1)
{
add();
}
else if(opt==2)
{
remove();
}
else if(opt==3)
{
update();
}
else if(opt==4)
{
search();
}
}
}
void add()
{
n++;
student s[n];
do
{
cout << "enter name of student ";
cin >> s[n-1].Name;
cout << "enter DoB of student ";
cin >> s[n-1].DoB;
cout << "enter age of student ";
cin >> s[n-1].Age;
cout << "enter address of student ";
cin >> s[n-1].Address;
cout << "enter courses and grades (max 5) \n";
for(int x=0;x<5;x++)
{
cout<<"course:";
cin>>s[n-1].Course[x];
cout<<"grade:";
cin>> s[n-1].grade[x];
}
cout << "Roll No. : " << n << "\nName :" << s[n-1].Name << "\nDoB :" << s[n-1].DoB << "\nAge :" << s[n-1].Age << "\nAddress :" << s[n-1].Address;
for( int x = 0; x < 5; x++ )
{
cout << "\n"<<s[n-1].Course[x];
cout << " grade : "<< s[n-1].grade[x]<<"\n";}
cout << "repeat for another student 'y' or 'n' ?";
cin >> opt1;
}
while(opt1=='y'|| opt1=='Y');
}
void remove()
{
student s[n];
do
{
cout << "enter student roll no to remove data";
cin >> l;
for( int i=l; i < n; i++ )
{
s[l-1].Name=s[l].Name;
s[l-1].DoB= s[l].DoB;
s[l-1].Age=s[l].Age;
s[l-1].Address=s[l].Address;
for(int j=0;j<5;j++)
{
s[l-1].Course[j]=s[l].Course[j];
s[l-1].grade[j]=s[l].grade[j];
}
}
cout<<"Record Removed\n";
n--;
cout << "repeat ? 'y' or 'n' ";
cin >> opt2;
}
while(opt2 == 'y' || opt2 == 'Y');
}
void update()
{
student s[n];
do
{
cout << "enter the roll no of student you want to update data";
cin >> u;
cout << "Roll No. : " << u;
cout << "\nName : ";
cout << s[u-1].Name;
cout << "\nDoB : ";
cout << s[u-1].DoB;
cout << "\nAge : ";
cout << s[u-1].Age;
cout << "\nAddress :";
cout << s[u-1].Address;
cout << "\nCourses and Grades\n";
for( int r=0; r < 5; r++ )
{
cout << s[u-1].Course[r];
cout << "\t"<< s[u-1].grade[r] << endl;
}
cout << "enter name of student ";
cin >> s[u-1].Name;
cout << "enter DoB of student ";
cin >> s[u-1].DoB;
cout << "enter age of student ";
cin >> s[u-1].Age;
cout << "enter address of student ";
cin >> s[u-1].Address;
cout << "enter courses and grades (max 5) \n";
for(int x=0; x < 5; x++ )
{
cout<<"course:";
cin>>s[u-1].Course[x];
cout<<"grade:";
cin>> s[u-1].grade[x];
}
cout << "Roll No. : " << u;
cout << "\nName : ";
cout << s[u-1].Name;
cout << "\nDoB : ";
cout << s[u-1].DoB;
cout << "\nAge : ";
cout << s[u-1].Age;
cout << "\nAddress :";
cout << s[u-1].Address;
cout << "\nCourses and Grades\n";
for( int r = 0; r < 5; r++)
{
cout << s[u-1].Course[r];
cout << "\t"<< s[u-1].grade[r]<<endl;
}
cout << "repeat ? 'y' or 'n' ";
cin >> opt3;
}
while(opt3 == 'y' || opt3 == 'Y');
}
void search()
{
student s[n];
do
{
cout << "enter the roll no of student you want to search data of";
cin >> y;
cout << "Roll No. : " << n << "\nName :" << s[y-1].Name << "\nDoB :" << s[y-1].DoB << "\nAge :" << s[y-1].Age << "\nAddress :" << s[y-1].Address << "\nCourses and Grades\n";
for( int r=0; r < 5; r++ )
{
cout << s[y-1].Course[r];
cout << "\t" << s[y-1].grade[r]<<endl;
}
cout << "repeat ? 'y' or 'n' ";
cin >> opt4;
}
while( opt4 == 'y' || opt4 == 'Y' );
}
The problem you're having is with the scope of your programme.
In the main() function you declare an array, which presumably should be holding all the records. However for every option (function add/remove/update/search) you declare a new one. These arrays are local to those functions and are being "forgotten" after the function is finished.
In order to fix that, you have to pass the original array (the one from main()) to those functions.
The next problem, which is more severe is the fact, that you can't change the size of an array in the runtime! The elements you were accessing in the 3rd option were some random bytes in the memory and this can cause a lot of trouble.
Either create an array with e.g. 100 fields ( student s[100]; ), or even better try using std::vector, which can size you can adjust according to the number of records.
Third thing, remember that formatting code is extremely important. Hopefully it was just a copy-paste problem that made your code look unreadable, but if it's not the case then try to change. Well formatted code is easier to understand and will make it easier for you to debug it.
I have a program that asks a user how many quizzes they would like to input, stores the grades, and the computes the average. The grade has to be between 0 and 100. However, when I enter a grade above 100 the average is wrong. It does not reset the sum. If I enter 120 and it prompts me to reenter the grades, it still uses 120 when calculating the average. How do I get it to reset?
Note: I do not want to change the the format (such as using a while loop).
#include <iostream>
using namespace std;
int main(){
int n, i;
float num[13], sum=0.0, average;
cout << "Enter the numbers of quizzes from 1-13: ";
cin >> n;
while (n < 1 || n > 13)
{
cout << "You must enter a number from 1 - 13." << endl;
cout << "Enter the number of quizzes you would like to input(1-13): “;
cin >> n;
}
for(i=0; i<n; ++i)
{
cout << i+1 << ". Enter number: ";
cin >> num[i];
sum+=num[i];
while (num[i] < 0 || num[i] > 100)
{
cout << “You must enter a number between 0 and 100. Re enter scores.”;
cout << i+1 << ". Enter number: ";
cin >> num[i];
}
}
average=sum/n;
cout << "Average = " << average;
return 0;
}
Just put the sum+=num[i] behind the while (num[i] < 0 || num[i] > 100) ... loop !
This way you only sum up valid numbers.
for(i=0; i<n; ++i)
{
cout << i+1 << ". Enter number: ";
cin >> num[i];
//sum+=num[i]; remove here !!
while (num[i] < 0 || num[i] > 100)
{
cout << “You must enter a number between 0 and 100. Re enter scores.”;
cout << i+1 << ". Enter number: ";
cin >> num[i];
}
sum+=num[i]; // <- insert here
}
for(i=0; i<n; ++i)
{
cout << i+1 << ". Enter number: ";
cin >> num[i];
while (num[i] < 0 || num[i] > 100)
{
cout << “You must enter a number between 0 and 100. Re enter scores.”;
cout << i+1 << ". Enter number: ";
cin >> num[i];
}
}
sum+=num[i];
average=sum/n;
cout << "Average = " << average;
return 0;
}
Okay so I when I run this code I get that my total is equal to 0 and that
messes up my average and grade.I am not sure what I am doing wrong as the
total += scores function is where it should be, yet it is still not adding up
the scores.
int validateNumber(int, int, int);
in main() function
int num, score, total = 0;
and
validateNumber(num, score, total);
and the definition
int validateNumber(int num, int score, int total) {
while (num < 1 || num > 4) {
cout << over3 << num << " is not between 1 and 4! Try again: ";
cin >> num;
}
system("CLS");
for (int i = 1; i <= num; i++) {
cout << over3 << "Enter score " << i << ": " << endl;
cout << over3 << "Enter a value from 0 to 100: ";
cin >> score;
while (score < 0 || score > 100) {
cout << over3 << score
<< " is not between 0 and 100! Renter the score: " << i << ": ";
cin >> score;
}
total += score;
}
return total;
}
If you want to implement the validate() function like you did here,
validateNumber(num,score,total);
you can make it void and pass the variable total as reference. e.g,
void validateNumber(int num, int score, int &total) {
while (num < 1 || num > 4) {
cout << over3 << num << " is not between 1 and 4! Try again: ";
cin >> num;
}
system("CLS");
for (int i = 1; i <= num; i++) {
cout << over3 << "Enter score " << i << ": " << endl;
cout << over3 << "Enter a value from 0 to 100: ";
cin >> score;
while (score < 0 || score > 100) {
cout << over3 << score
<< " is not between 0 and 100! Renter the score: " << i << ": ";
cin >> score;
}
total += score;
}
}
and the rest would be same...
Otherwise I wouldn't have use 3 arguments in this case. e.g,
int validateNumber(int num) {
int total=0,score;
while (num < 1 || num > 4) {
cout << over3 << num << " is not between 1 and 4! Try again: ";
cin >> num;
}
system("CLS");
for (int i = 1; i <= num; i++) {
cout << over3 << "Enter score " << i << ": " << endl;
cout << over3 << "Enter a value from 0 to 100: ";
cin >> score;
while (score < 0 || score > 100) {
cout << over3 << score
<< " is not between 0 and 100! Renter the score: " << i << ": ";
cin >> score;
}
total += score;
}
return total;
}
and the call:
int num, total;
...
total=validateNumber(num);
Hope it helped...
Are you assuming the function call validateNumber(num,score,total); in line 5 would calculate the total? You should call the function from main function, and assign the return value to a variable, (e.g. total).