Picture of error when input is jst L
#include <iostream>
using namespace std;
int main()
{
string student_name;
cout << "Enter student name\n";
cin >> student_name;
string student_surname;
cout << "Enter Student surname\n";
cin >> student_surname;
string student_id;
cout << "Enter Student ID\n";
cin >> student_id;
string student_group;
cout << "Enter Student Group\n";
cin >> student_group;
string Module_code;
cout << "Enter Module Code\n";
cin >> Module_code;
float test1;
cout << "Enter Test 1 mark\n";
cin >> test1;
float test2;
cout << "Enter Test 2 mark\n";
cin >> test2;
float final_mark;
final_mark = (test1 + test2) / 2;
cin >> final_mark;
string grade;
if (final_mark >= 80) {
grade = "A";
cin >> grade;
}
else if (final_mark >= 70) {
cout << "B";
cin >> grade;
}
else if (final_mark >= 60) {
grade = "C";
cin >> grade;
}
else if (final_mark >= 50) {
grade = "D";
cin >> grade;
}
else if (final_mark >= 40) {
grade = "E";
cin >> grade;
}
else (final_mark <= 30);
{
grade = "F";
cin >> grade;
} /*final code*/ cout << "LOMKOWKING UNIVERSITY\n";
cout << "ESWATINI CAMPUS\n";
cout << "STUDENT RESULT\n";
cout << "STUDENT NAME :" << cout << student_surname << student_name << endl;
cout << "STUDENT ID :" << cout << student_id << endl;
cout << "STUDENT GROUP :" << cout << student_group << endl;
cout << "MODULE CODE :" << cout << Module_code << endl;
cout << "Test 1 :" << cout << test1;
cout << "Test 2" << cout << test2 << endl;
cout << "Final Mark :" << cout << final_mark;
cout << "Grade :" << cout << grade << endl;
return 0;
}
#include <bits/stdc++.h>
using namespace std;
int main() {
string student_name;
cout<< "Enter student name\n";
cin>>student_name;
string student_surname;
cout<<"Enter Student surname\n";
cin>>student_surname;
string student_id;
cout<<"Enter Student ID\n";
cin>>student_id;
string student_group;
cout<<"Enter Student Group\n";
cin>>student_group;
string Module_code;
cout<<"Enter Module Code\n";
cin>>Module_code;
float test1;
cout<<"Enter Test 1 mark\n";
cin>>test1;
float test2;
cout<<"Enter Test 2 mark\n";
cin>>test2;
float avg;
avg = (test1 + test2) / 2;
string grade;
if(avg<=40){
grade = "F";
}
else if(avg<=50 && avg>40){
grade = "E";
}
else if(avg<=60 && avg>50){
grade = "D";
}
else if(avg<=70 && avg>60){
grade = "C";
}
else if(avg<80 && avg>70){
grade = "B";
}
else if(avg>=80){
grade = "A";
}
/*final code*/
cout<< "LOMKOWKING UNIVERSITY\n";
cout<<"ESWATINI CAMPUS\n";
cout<< "STUDENT RESULT\n";
cout<< "STUDENT NAME :" <<student_surname << " " <<student_name << endl;
cout<< "STUDENT ID :" <<student_id << endl;
cout<< "STUDENT GROUP :"<<student_group << endl;
cout<< "MODULE CODE :"<<Module_code << endl;
cout<< "Test 1 :" <<test1<<endl;
cout<< "Test 2: "<<test2 << endl;
cout<< "Final score :"<<avg << endl;
cout<< "Grade :"<<grade<<endl;
return 0;
}
please, do not input "L" as a value to float variables (i.e. test1 and test2)
Related
I am currently working on a personal project. For now, I am creating the part where it gets the users information. I am running into an issue where if the user chooses to correct their name, it steps over it and does not let the user re input the correct name. I have tried clearing before getting to the switch statement and in different locations.
Here is the switch statement:
switch (correction)
{
case 1 : cout << "Name: ";
cin.clear();
getline(cin,name);
checkInfo(age,number,name);
break;
case 2 : cout << "Age: ";
cin >> age;
while (age < 21)
{
cout << "Age is too low, try again." << endl;
cin >> age;
}
checkInfo(age,number,name);
break;
case 3 : cout << "Number: ";
cin >> number;
while(!isNumeric(number) || number.size() < 8)
{
cout << "Phone number either is too short/long or contains characters that are not digits. Try again" << endl;
cout << "Number: ";
cin >> number;
}
checkInfo(age,number,name);
break;
default : cout << "Please select options 1 through 3, nothing else is accepted: ";
cin >> correction;
break;
}
And here is where the user first in puts their information in the beginning:
cout << "Enter your name: ";
getline(cin,name);
cin.clear();
cout << "Enter your age: ";
cin >> age;
cin.clear();
cout << "Enter your phone number: ";
cin >> number;
cout << endl;
while (age >= 21)
{
while (!isNumeric(number) || number.size() < 8)
{
cout << "Phone number either is too short/long or contains characters that are not digits. Try again" << endl;
cout << "Number: ";
cin >> number;
}
checkInfo(age,number,name);
}
}
I have a feeling i'm not clearing it somewhere correctly. Any help or recommendations to make this better is also appreciated. Thank you.
info.h file:
//info.h file
#ifndef INFO
#define INFO
#include <iostream>
#include <fstream>
#include <cctype>
#include <algorithm>
using namespace std;
namespace info
{
class user
{
public:
char yesno;
char confirm;
int correction;
void getInfo (int age, string number, string name);
void checkInfo(int age, string number, string name);
void changeInfo(int age, string number, string name);
private:
bool isNumeric(string str);
};
}
void info::user::changeInfo(int age, string number, string name)
{
cout << "Age: " << age << endl;
cout << endl;
cout << "Please select the number according to the information that needs to be changed." << endl;
cout << endl;
cout << "1. Name" << endl;
cout << "2. Age" << endl;
cout << "3. Number" << endl;
cout << "Selection: ";
cin >> correction;
switch (correction)
{
case 1 : cout << "Name: ";
cin.clear();
getline(cin,name);
checkInfo(age,number,name);
break;
case 2 : cout << "Age: ";
cin >> age;
while (age < 21)
{
cout << "Age is too low, try again." << endl;
cin >> age;
}
checkInfo(age,number,name);
break;
case 3 : cout << "Number: ";
cin >> number;
while(!isNumeric(number) || number.size() < 8)
{
cout << "Phone number either is too short/long or contains characters that are not digits. Try again" << endl;
cout << "Number: ";
cin >> number;
}
checkInfo(age,number,name);
break;
default : cout << "Please select options 1 through 3, nothing else is accepted: ";
cin >> correction;
break;
}
}
void info::user::getInfo (int age, string number, string name)
{
cout << "Enter your name: ";
getline(cin,name);
cin.clear();
cout << "Enter your age: ";
cin >> age;
cin.clear();
cout << "Enter your phone number: ";
cin >> number;
cout << endl;
while (age >= 21)
{
while (!isNumeric(number) || number.size() < 8)
{
cout << "Phone number either is too short/long or contains characters that are not digits. Try again" << endl;
cout << "Number: ";
cin >> number;
}
checkInfo(age,number,name);
}
}
void info::user::checkInfo(int age, string number, string name)
{
cout << "Is the given information correct?" << endl;
cout << "-------------------" << endl;
cout << endl;
cout << "Name: " << name << endl;
cout << "Age: " << age << endl;
cout << "Number: " << number << endl;
cout << endl;
cout << "If yes, please press y for yes, n for no: ";
cin >> confirm;
while (age >= 21)
{
if (confirm == 'y' || confirm == 'Y')
{
cout << "In my actual project, this is where i would save the information" << endl;
break;
}
else
{
changeInfo(age,number,name);
checkInfo(age,number,name);
break;
}
}
}
bool info::user::isNumeric(string str)
{
for (int i = 0; i < str.length(); i++)
{
if(isdigit(str[i]) == false)
{
return false;
}
}
return true;
}
#endif
Main.cpp file:
#include <iostream>
#include "info.h"
using namespace std;
int main ()
{
int age;
string number;
string name;
info::user userInfo;
userInfo.final(age, number,name);
return 0;
}
I am new to c++ and my textbook is not very helpful. I have a few errors in my code. Where I am being told the identifier for customerAccount is undefined, and I have an incompatible declaration with my int search before and after my main. I will post some code below as I have been trying to figure this out for a while and I am at a loss.
#include<iostream>
#include<string>
using namespace std;
struct {
string Name;
string Address;
string City_State_Zip;
double phoneNumber;
double actBalance;
string Payment;
};
//This is where the errors start, saying customer account is undefined
void Display(customerAccount ca);
//declaration is incompatible with int search
int Search(customerAccount, string);
int main() {
customerAccount customers[10];
string SName;
int choice, i = 0, size = 0;
do {
cout << "****Menu****" << endl;
cout << "1. Enter Customer Information" << endl;
cout << "2. Change Customer Information" << endl;
cout << "3. Search For Customer" << endl;
cout << "4. Display Customer Data" << endl;
cout << "5. Exit" << endl;
cout << "Please enter a choice 1,2,3,4 or 5";
cin >> choice;
switch (choice) {
case 1:
cout << "Enter customer name: ";
cin >> customers[i].Name;
cout << "Enter customer address: ";
cin >> customers[i].Address;
cout << "Enter city state and zip: ";
cin >> customers[i].City_State_Zip;
cout << "Enter phone number: ";
cin >> customers[i].phoneNumber;
cout << "Enter account balance: ";
cin >> customers[i].actBalance;
if (customers[i].actBalance < 0) {
cout << "Account balance cannot be negative. Please re-enter: "
<< endl;
cin >> customers[i].actBalance;
}
cout << "Enter last payment: ";
cin >> customers[i].Payment;
i++;
break;
case 2: int ele;
cout << "Enter customer information to modify: " << endl;
cout << "Enter customer name: ";
cin >> customers[ele - 1].Name;
cout << "Enter customer address: ";
cin >> customers[ele - 1].Address;
cout << "Enter city state and zip";
cin >> customers[ele - 1].City_State_Zip;
cout << "Enter phone number: ";
cin >> customers[ele - 1].phoneNumber;
cout << "Enter account balance: ";
cin >> customers[ele - 1].actBalance;
if (customers[ele - 1].actBalance < 0) {
cout << "Account balance cannot be negative. Please re-enter: "
<< endl;
cin >> customers[i].actBalance;
}
cout << "Enter date of payment: ";
cin >> customers[ele - 1].Payment;
break;
case 3: cout << "Enter customer name to search: ";
cin >> SName;
for (size = 0; size < i; size++) {
int check;
check = Search (customers[size], SName);
if (check == 1)
Display(customers[size]);
}
break;
case 4:
for (size = 0; size < i; size++)
Display(customers[size]);
break;
case 5: exit(0);
break;
}
} while (choice != 5);
system("pause");
return 0;
}
void Display(customerAccount ca) {
cout << " Customer name:";
cout << ca.Name << endl;
cout << " Address:";
cout << ca.Address << endl;
cout << " city state and zip:";
cout << ca.City_State_Zip << endl;
cout << " Phone number:";
cout << ca.phoneNumber << endl;
cout << " Account balance:";
cout << ca.actBalance << endl;
cout << " Date of payment:";
cout << ca.Payment << endl;
}
//declaration is incompatible with int search
int Search(customerAccount ca, string str) {
if (str.compare(ca.Name) == 0)
return 1;
else
return 0;
}
case 2: int ele;
On this line you have an uninitialized variable which is causing your bug.
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);
}