I am trying to get this program to input my names to run with the number of students I input. However, it will not allow me to. Any help would be great, here is what I have so far.
//setting limit to number of students
const int MAX_STU = 24;
//declaring functions
int getStuCount();
int getStuName(int num, string name);
int getExamScores(int num, int exam[]);
int main(int argc, char *argv[])
{
//declaring arrays
string name;
int exam[MAX_STU];
int num = getStuCount();
getStuName(num, name);
getExamScores(num, exam);
system("PAUSE");
return EXIT_SUCCESS;
}
//getting number of students
int getStuCount()
{
int num = 0;
cout << "Enter the number of students: ";
cin >> num;
return num;
}
//getting names of students
int getStuName(int num, string name)
{
for(int i = 0; i < num; i++)
{
cout << "Enter the name of student" << (i + 1) << ":" << endl;
cin >> name[i];
}
return 0;
}
//get the exam scores for the students
int getExamScores(int num, int exam[])
{
for (int i = 0; i < num; i++)
{
do
{
cout << "Enter the exam score of student " << (i + 1) << "(0-100): ";
cin >> exam[i];
} while (exam[i] < 0 || exam[i] > 100); // check range 0-100
}
return 0;
}
As of now when I run the code, I only can enter one character. I thought a string was an infinite value of characters? How would I be able to be able to enter a name?
It sounds like you actually want a list of names (I'm guessing 24 of them?).
In array form:string names[MAX_STU];
Given an array form there as I'm assuming you're unfamiliar with vectors or classes. I'd look them up, though. They'd make your life a lot easier on something like this. :-)
Yes, I am trying to make a program to enter 24 names and find the averages of their lab and exam scores. I was getting stuck on imputing their names and allowing it to continue. Thank you for your help, I'm sure I'll be needing more later.
As far as classes go, I am a little foggy on it as my last programming class was over a year ago and I was just thrown into this one. I will be going over my notes again to try and get back in the swing of things.
P.S. how did you compiled it? Without errors or warnings?
You in main() function create variable name which is of type string, but don't give any value and give this variable to function getStuName(). If you do not give a value to a variable you can give to function only variables address, not variable.
#include <cstdlib>
#include <iostream>
#include <string>
usingnamespace std;
//declaring functions
int getStuCount();
int getStuName(int num, string *name);
int getExamScores(int num, int *exam);
int main()
{
int num = getStuCount();
//declaring arrays
int *exam = newint[num];
string *name = new string[num];
getStuName( num, name);
getExamScores(num, exam);
for(int i = 0 ; i < num; i++)
{
cout<<"-Student "<<i+1<<" Name : "<<name[i]<<endl
<<"Exam : "<<exam[i]<<endl<<endl;
}
delete []name;
delete []exam;
system("PAUSE");
return EXIT_SUCCESS;
}
//getting number of students
int getStuCount()
{
int num = 2;
while(true)
{
num = 0;
cout << "Enter the number of students: ";
cin >> num;
if(num>24||num<=0)
{
cout<<"Enter number 1-24 SUCKER!!!"<<endl;
}
else
{
break;
}
}
return num;
}
//getting names of students
int getStuName(int num, string* name)
{
for(int i = 0; i < num; i++)
{
do
{
cin.clear();
cin.sync();
cout << "Enter the name of student" << (i + 1) << ":" << endl;
getline(cin,name[i]);
}while(name[i]=="");
}
return 0;
}
//get the exam scores for the students
int getExamScores(int num, int *exam)
{
for (int i = 0; i < num; i++)
{
do
{
cout << "Enter the exam score of student " << (i + 1) << "(0-100): ";
cin >> (exam[i]);
} while (exam[i] < 0 || exam[i] > 100); // check range 0-100
}
return 0;
}