I want to call the void getInput function in the main scope. But when I do this, it tells me:
too few argument in function call.
How do I fix this?
The first void function prints the exercises. Then I call it in the next void function called getInput. After that I just want to call it in the main() function.
#include <iostream>;
#include <string>;
using namespace std;
void Exercices()
{
double speed;
int minutes;
cout << "walking: ";
cin >> speed >> minutes;
cout << "running: ";
cin >> speed >> minutes;
cout << "cycling: ";
cin >> speed >> minutes;
}
void getInput(string username)
{
double weight, goal;
string walking, running, cycling;
cout << "Please enter your name: ";
cin >> username;
cout << "Welcome " << username << ", please enter your weight(kg): ";
cin >> weight;
cout << username << ", please enter speed(km/h) and minutes spent in a week for the activities below." << endl;
Exercices();
cout << username << ", please enter your weekly calorie burn goal: ";
cin >> goal;
}
int main()
{
//string user_info;
getInput();
Exercices();
cout << endl;
return 0;
}
As the error suggests,
int main()
{
string user_info;
getInput(user_info);
Exercices();
cout << endl;
return 0;
}
You have to pass a string to the function getInput(string username) since the function definition says it needs one. I hope you will read and try to understand the error message before everything else in the future
Related
So I am trying to call a function which creates an assessment class object within the newStudent function. Ideallly the aim is for the newStudent function to run and after it reaches level selection it would call the respective makeAssess functions. Each of the makeAssess functions would then recursively call themselves until the total weighting of each level is 100 and then move onto the next makeAssess function. All the makeAssess functions were already defined at the start of the program.
Edit: Iam using CodeBlocks with a GNU GCC compiler. The two errors are "|undefined reference to `makeassessH()'|" and "|error: ld returned 1 exit status|"
MPE:
//Relevant libraries
#include <iostream>
#include <fstream>
using namespace std;
int option;
void newStudent();
void makeassessH();
void makeassessI();
void makeassessC();
class Student {
public:
int stuNum;
std::string stuName;
int startDate;
int endDate;
string progofStudy;
char level;
};
class Assessment{
public:
std::string title;
int weight;
double grade;
string deadline;
};
int main()
{
//The option page
while (option != 5) {
cout << "\nPlease choose an option\n"
"1. Add new students\n"
"2. Student Login\n"
"3. Staff Login\n"
"4. Admin View\n"
"5. Exit\n"
"Enter option: ";
cin >> option;
if (option == 1) {
newStudent();
} else if (option == 5)
return 0;
}
}
void newStudent() {
Student stu;
string studentName = stu.stuName;
cout << "Please enter in your student number: ";
cin >> stu.stuNum;
cout << "Please enter in your name: ";
cin.ignore();
getline (cin, studentName);
cout << "Please enter in your start date: ";
cin >> stu.startDate;
cout << "Please enter in your end date: ";
cin >> stu.endDate;
cout << "Please enter in your programme of study: ";
cin.ignore();
getline(cin, stu.progofStudy);
cout << "Please enter your level of study: ";
cin >> stu.level;
if(stu.level == toupper('h')){
makeassessH();
makeassessI();
makeassessC();
}else if (stu.level == toupper('i')){
makeassessI();
makeassessC();
}else if(stu.level == toupper('c')) {
makeassessC();
}
}
void makeassesH(){
Assessment assessmentH;
cout << "Assessment title: ";
cin.ignore();
getline (cin, assessmentH.title);
cout << "Assessment weighting: ";
cin >> assessmentH.weight;
cout << "Assessment grade: ";
cin >> assessmentH.grade;
cout << "Assessment deadline: ";
cin >> assessmentH.deadline;
}
void makeassessI(){
Assessment assessmentI;
cout << "Assessment title: ";
cin.ignore();
getline (cin, assessmentI.title);
cout << "Assessment weighting: ";
cin >> assessmentI.weight;
cout << "Assessment grade: ";
cin >> assessmentI.grade;
cout << "Assessment deadline: ";
cin >> assessmentI.deadline;
}
void makeassessC(){
Assessment assessmentC;
cout << "Assessment title: ";
cin.ignore();
getline (cin, assessmentC.title);
cout << "Assessment weighting: ";
cin >> assessmentC.weight;
cout << "Assessment grade: ";
cin >> assessmentC.grade;
cout << "Assessment deadline: ";
cin >> assessmentC.deadline;
int totalWeight = totalWeight + assessmentC.grade;
}
The error message is telling you (and us all now) that you have declared a function named
void makeassessH()
but you never defined it. You do have a function named
void makeassesH()
but that's not the same spelling.
Good day everyone
I am new to programming and C++.
I have been having some trouble with my program. I want the function culcAverageYearMark to use the variables from the function getMarks but no matter what I have tried it always throw any error like "expected primary expression before 'int' " I have tried everything that I possibly can but no lucky.
#include <iostream>
using namespace std;
void studentDetails()
{
string name, surName, schoolName;
cout << "Enter your name: \n";
cin >> name;
cout << "Enter your surname: \n";
cin >> surName;
cout << "Enter your school name: \n";
cin >> schoolName;
}
void getMarks()
{
int english, mathematics, lifeOrientation, history, computerLiteracy, geography;
cout << "Enter your mark for English: \n";
cin >> english;
while (!(english >0 && english <= 100))
{
cout << "Invalid mark \n";
cout << "Please enter your mark for English: \n";
cin >> english;
}
cout << "Enter your mark for Mathematics: \n";
cin >> mathematics;
while (!(mathematics >0 && mathematics <= 100))
{
cout << "Invalid mark \n";
cout << "Please enter your mark for Mathematics: \n";
cin >> mathematics;
}
cout << "Enter your mark for Life Orientation: \n";
cin >> lifeOrientation;
while (!(lifeOrientation >0 && lifeOrientation <= 100))
{
cout << "Invalid mark \n";
cout << "Please enter your mark for lifeOrientation: \n";
cin >> lifeOrientation;
}
cout << "Enter your mark for History: \n";
cin >> history;
while (!(history >0 && history <= 100))
{
cout << "Invalid mark \n";
cout << "Please enter your mark for History: \n";
cin >> history;
}
cout << "Enter your mark for Computer Literacy: \n";
cin >> computerLiteracy;
while (!(computerLiteracy >0 && computerLiteracy <= 100))
{
cout << "Invalid mark \n";
cout << "Please enter your mark for Computer Literacy: \n";
cin >> computerLiteracy;
}
cout << "Enter your mark for Geography: \n";
cin >> geography;
while (!(geography >0 && geography <= 100))
{
cout << "Invalid mark \n";
cout << "Please enter your mark for Geography: \n";
cin >> geography;
}
}
void calcAverageYearMark();
int main()
{
studentDetails();
getMarks();
calcAverageYearMark();
return 0;
}
void calcAverageYearMark()
{
getMarks(int english, int mathematics, int lifeOrientation, int history, int computerLiteracy, int geography)
float average = (english + mathematics + lifeOrientation + history + computerLiteracy + geography)/6;
}
Yes this is a scope issue.
If you want to calculate something in one function then get that value and send it to another function you want to use this code example...
int calculationA()
{
int a;
//do calulation to a
return a;
}
int calculateB(int b)
{
//b was sent from the main method
//we can use it here
return b;
}
int main()
{
int x = calculationA();
int output = calculationB(x);
//output variable now contains the caclulations after going through both methods
return 0;
}
you cannot access the variable without either making it global or sending it as a parameter. You are experiencing what is called a scope error
The problem you are having really has a couple different causes.
First of all, your function get_marks() has a void return type. That means that it does not return any value when it is run. If you want to get a value out of it you will need to give it a return type. For you needs it would need to return either a reference to an array or a pointer. If you don't know what those are then you should rethink how you are structuring your code so that you aren't getting multiple values out of a single function.
Another big issue is that you are calling get_marks incorrectly. The way you have defined it does not take any argument, so you will run into compilation issues because you are trying to pass it arguments when it does not expect any. You are also not properly passing arguments in the first place. You should not put a datatype before arguments.
Here is a simple rewrite of your program that should get the results you want:
#include <iostream>
using namespace std;
void studentDetails()
{
string name, surName, schoolName;
cout << "Enter your name: \n";
cin >> name;
cout << "Enter your surname: \n";
cin >> surName;
cout << "Enter your school name: \n";
cin >> schoolName;
}
int getMark(const string &class)
{
int english, mathematics, lifeOrientation, history, computerLiteracy, geography;
cout << "Enter your mark for " << class << " \n";
cin >> val;
while (!(val >0 && val <= 100))
{
cout << "Invalid mark \n";
cout << "Please enter your mark for English: \n";
cin >> val;
}
return val
}
float calcAverageYearMark()
{
int english, mathematics, lifeOrientation, history, computerLiteracy, geography;
english = getMark("english");
mathematics = getMark("mathematics");
lifeOrientation = getMark("lifeOrientation");
history = getMark("history");
computerLiteracy = getMark("computerLiteracy");
geography = getMark("geography");
float average = (english + mathematics + lifeOrientation + history + computerLiteracy + geography)/6.0;
return average;
}
int main()
{
studentDetails();
getMarks();
calcAverageYearMark();
return 0;
}
Please note that I have not tested this code and it is more meant to demonstrate a better structure to accomplish what you are trying to do.
I am writing a program and it is a long program and I have just started. I just tested it for running, Please tell me why it is not having user input:
#include <iostream>
using namespace std;
struct courses{
int CLO1, CLO2, CLO3, CLO4, CLO5;
int tcounter;
};
int main(){
cin.clear();
courses C1;
C1.CLO1;
C1.CLO2;
C1.CLO3;
int counter = 0;
char name;
cout << "Enter your Name: ";
cin >> name;
cout << "For C1, we have three CLOs that CLO1,CLO2 and CLO3. CLO1 and CLO3 are linked to PLO1 and CLO2 is linked to PLO2 " << endl;
cout << "Enter your marks in CLO1 for C1:(Out of 10) ";
cin >> C1.CLO1;
if (C1.CLO1 >= 5){
counter++;
}
cout << "Enter your marks in CLO2 for C1:(Out of 10) ";
cin >> C1.CLO2;
if (C1.CLO2 >= 5){
counter++;
}
cout << "Enter your marks in CLO3 for C1:(Out of 10) ";
cin >> C1.CLO3;
if (C1.CLO3 >= 5){
counter++;
}
cout << counter;
return 0;
}
As per me the program is fine.
Three things you need to change:
variable type of name from char to string
C1.CLO1;C1.CLO2;C1.CLO3; remove these from your code it doesn't make any sense.
Print the values and check :P
Everything seems to check out in my code; I've checked syntax on the different functions and everything seems to be checking out but I still seem to be getting the error 'No Matching Function for Call to 'MyInput'' in XCode with no explanation. Any help would be appreciated! Code follows.
#include <iostream>
#include <iomanip>
using namespace std;
typedef char ShortName[10];
typedef char LongName[17];
typedef char IDarray[6];
void MyInput(ShortName [],char [], LongName [], float [],int [],IDarray [],int&);
int main(int argc, const char * argv[])
{
const int max = 7;
const int ConstStateTax = .05;
const int ConstFedTax = .15;
const int ConstUnionFees = .02;
ShortName firstname[max];
char MI[max];
LongName lastname[max];
float hourrate[max];
int OTHours[max];
float Gross[max];
float Overtime[max];
float GGross[max];
float StateTax[max];
float FedTax[max];
float UnionFees[max];
IDarray EmployeeID[max];
float Net[max];
MyInput(firstname,MI,lastname,hourrate,OTHours,EmployeeID,max);
return 0;
}
void MyInput(ShortName firstname[],char MI[],LongName lastname[],float hourrate[],int OTHours[],IDarray EmployeeID[],int &max)
{
for(int i = 0;i<=max;i++)
{
cout << "Please enter the first name of employee #" << i+1 << ": ";
cin >> firstname[i];
cout << "Please enter the middle initial of employee #" << i+1 << ": ";
cin >> MI[i];
cout << "Please enter the last name of Employee #" << i+1 << ": ";
cin >> lastname[i];
cout << "What is the ID number of Employee #" << i+1 << "? (6 letters or numbers only): ";
cin >> EmployeeID[i];
cout << "What is the hourly rate of employee #" << i+1 << "? ";
cin >> hourrate[i];
while (hourrate[i] < 0)
{
cout << "Invalid rate, please try again: ";
cin >> hourrate[i];
}
cout << "How many overtime hours does employee #" << i+1 << " have? ";
cin >> OTHours[i];
while(OTHours[i]<0 || OTHours[i] >20)
{
cout << "Invalid Overtime Hours, please re-enter: ";
cin >> OTHours[i];
}
}
}
the function you are trying to call is, which takes int& as last argument
void MyInput(ShortName [],char [], LongName [], float [],int [],IDarray [],int&);
but you pass const int max to it, which have type of const int and can't convert to int&
to fix it, change int& to int for both method declaration and definition
I know this maybe such a wierd question but it stucks me for a couple of days ago. My assignemnt is to display the students' information and update them using STRUCT type. Here is my works:
#include <iostream>
using namespace std;
struct DATE
{
int day;
int month;
int year;
};
struct STUDENT{
char ID[8];
char name[50];
DATE birthday;
char address[100];
float Math;
float English;
float CS;
};
void inputClass(STUDENT* &list, int &n)
{
cout << "Please enter the number of students: ";
cin >> n;
list = new STUDENT[n+1];
for(int i=1; i<=n; i++)
{
cout << "Please enter the info of student " << i << endl;
cout << "ID: ";
cin >> (&list[i]) -> ID; //the same with "list[i].ID"
fflush(stdin);
cout << "Name: ";
cin >> (&list[i]) -> name;
fflush(stdin);
cout << "Date of Birth\n";
cout << "Day: ";
cin >> (&list[i]) -> birthday.day;
fflush(stdin);
cout << "Month: ";
cin >> (&list[i]) -> birthday.month;
fflush(stdin);
cout << "Year: ";
cin >> (&list[i]) -> birthday.year;
fflush(stdin);
cout << "Address: ";
cin >> (&list[i]) -> address;
fflush(stdin);
cout << "Math result: ";
cin >> (&list[i]) -> Math;
fflush(stdin);
cout << "English result: ";
cin >> (&list[i]) -> English;
fflush(stdin);
cout << "CS result: ";
cin >> (&list[i]) -> CS;
fflush(stdin);
cout << "************* Next Student *************\n" ;
}
}
void updateScore(STUDENT* list, int n)
{
cout << "Who do you want to update?" << endl;
cout << "Ordinal Number(s): ";
cin >> n;
//Display outdated results
cout << "Student's Name: " << (&list[n])-> name << endl;
cout << "*********** Current Results ***********" << endl;
cout << "Math: " << (&list[n]) -> Math << endl;
cout << "English: " << (&list[n]) -> English << endl;
cout << "CS: " << (&list[n]) -> CS << endl;
//Update results
cout << "Please update the results" << endl;
cout << "Math result: ";
cin >> (&list[n]) -> Math;
fflush(stdin);
cout << "English result: ";
cin >> (&list[n]) -> English;
fflush(stdin);
cout << "CS result: ";
cin >> (&list[]) -> CS;
fflush(stdin);
}
void main()
{
STUDENT* list;
int n;
inputClass(list, n);
updateScore(list, n);
}
In the "//Display outdated result" section, I used "cout" to print out the Name of the regarding student based on his/her ordinal numbers. However, let's say I want to get the whole name like: "John Smith". What I have got, however, is only "John". Is there a way I can get all of the characters?
Many thanks for your help and sorry for my bad English, I am a student from Vietnam.
Use std::getline from the <string> header, with a std::string variable, instead of >> and raw character array.
The >> reads whitespace-separated words of input.
The raw character array doesn't adjust to the needed length, and you risk Undefined Behavior on buffer overflow.
In passing, many/most programmers find all UPPERCASE to be an eyesore; it hurts the eyes.
Also, all uppercase is by convention (in C and C++) reserved for macro names.
As it's already been answered previously, you should use std::getline (refer to this question).
I'm assuming you're using a IDE, and it usually fixes a lot of things for us, users, but this may turn your code non-compilable in other compilers, so there are some things you should fix to be able to compile your code everywhere:
Always pay attention if you're adding the necessary includes. There's lack of an include statement for stdin and fflush. You should add:
#include <cstdio>
Also, main should return an int, so, it should have been something like
int main(int argc, char* argv[]){ /*Although you can usually omit the parameters*/
// Code
return 0;
}
By the way, just as a side comment, you forgot the subscript at:
cout << "CS result: ";
cin >> (&list[]) -> CS;