I have created an array that holds 5 numbers, and the user inputs the numbers. If the mark is less than 0 and greater than 100, I want to print out "invalid mark number". How could I do that?
using namespace std;
int mark[5];
int main ()
{
cout << "enter mark 0: ";
cin >> mark[0];
cout << "enter mark 1: ";
cin >> mark[1];
cout << "enter mark 2: ";
cin >> mark[2];
cout << "enter mark 3: ";
cin >> mark[3];
cout << "enter mark 4: ";
cin >> mark[4];
}
You should use a for-loop to make the code more readable and compact. Because once you introduce if statements, the code size would grow alot. It should look like this:
#include <iostream>
using namespace std;
int mark[5];
int main () {
for (int i = 0; i < 5; i++){
cout << "enter mark " << i << ": ";
cin >> mark[i];
if (mark[i] < 0 || mark[i] > 100){
cout << "invalid mark number\n";
}
}
}
Don't use using namespace std; (read here why) and keep the int mark[5]; inside the main-function (read here why). Also to add to the logic force the user to input again:
#include <iostream>
int main () {
int mark[5];
for (int i = 0; i < 5; i++){
bool valid_input = false;
while (!valid_input){
std::cout << "enter mark " << i << ": ";
std::cin >> mark[i];
if (mark[i] < 0 || mark[i] > 100){
std::cout << "invalid mark number\n";
}
else{
valid_input = true;
}
}
}
}
Related
I'm new to c++ so my current code might be completely wrong.
I'm trying to get two inputs into two arrays I've declared two arrays of max size 10 and I want to use the console input and add it to the end of each array.
#include <iostream>
int main() {
char playerName[10];
char playerScore[10];
char name, score;
cout << "Enter the player name:";
cin >> name;
cout << "Enter the player score"
cin >> score;
for (i=0; i<10, i++)
{
// add name at the end of playerName
// add score at the end of playerScore
}
return 0;
}
First off, use string not char if you want multiple characters. You also need to initialize i first.
Then you can add inputs at the end of your array like this;
int main() {
string playerName[10];
int playerScore[10];
for (int i=0; i<10; i++)
{
cout << "Enter the player name: ";
cin >> playerName[i];
cout << "Enter the player score: ";
cin >> playerScore[i];
}
}
If you want to use character arrays, they should be 2d arrays, such as char playername [10][10].
Maximum name of 10 characters.
#include <iostream>
using namespace std;
int main() {
char playerName[10][10];
int playerScore[10];
for (int i=0; i<10; i++)
{
cout << "Enter the "<<i+1<<"th "<< "player's name:";
cin >> playerName[i];
cout << "Enter the " <<i+1<<"th "<< "player's score:";
cin >>playerScore[i];
cout << "_________________________________________\n";
}
return 0;
}
1- you must use using namespace std; after #include <iostream>
2- you must use ; at the end of cout << "Enter the player score"
3- you must specify the type of i when you use for loop and use ; between each part -> for (int i = 0; i < 10; i++)
and for fill the character array use -> cin >> playerName; and cin >> playerScore;
#include <iostream>
using namespace std;
int main() {
int n = 2;
char playerName[10];
char playerScore[10];
char name, score;
for (int i = 0 ; i < n; i++)
{
cout << "Enter the player name: ";
cin >> playerName;
cout << "Enter the player score: ";
cin >> playerScore;
cout << playerName <<'\n';
cout << playerScore << endl;
}
return 0;
}
But it is better to use a string
I want to promt for a string variable (name of item) and then promt for a double variable (cost). I want this to be done 5 times so each time it loops the vales are stored as a different pair of variable.
need to have user input an item and then its price so i can calc a bill.
not sure if I can crate a loop for this or i need to keep a running count somehow
int main()
{
int i;
string Item_1,Item_2,Item_3,Item_4,Item_5;
double Price_1,Price_2,Price_3,Price_4,Price_5 ;
while (i<6)
{
cout<<"Please enter item"<<endl;
cin>> Item_1>>Item_2>>Item_3>>Item_4>>Item_5>>endl;
cout<<"Please enter cost of " >> Item_1>>Item_2>>Item_3>>Item_4>>Item_5;
cin>>Price_1>>Price_2>>Price_3>>Price_4>>Price_5;
i=i++
}
return 0;
}
Code doesn't compile but i expect it to ask for my in put for the 5 variables 5 times
Here is a solution with arrays and a for loop.
You can try it in CPP Shell.
#include <iostream>
#include <string>
using namespace std;
int main()
{
string Item[5];
double Price[5] ;
for(int i = 0; i < 5; i++)
{
cout<<"Please enter item"<<endl;
cin>> Item[i];
cout<<"Please enter cost of " << Item[i] << ":" << endl;
cin>>Price[i];
}
cout << "Items: ";
for(int i = 0; i < 5; i++)
{
cout << Item[i] << " ";
}
cout << endl << "Prices: ";
for(int i = 0; i < 5; i++)
{
cout << Price[i] << " ";
}
return 0;
}
Your code doesn't compile for many reasons!
Of course you can use a loop for your purpose. Consider the following code, which is not really good, but does the job:
#include <string>
#include <iostream>
using namespace std;
int main()
{
int i = 1;
string Item[5];
double Price[5];
while (i<6)
{
cout<<"Please enter item "<< i << ": ";
cin>>Item[i-1];
cout<<endl;
cout <<"Please enter cost of "<< Item[i-1] << ": ";
cin >> Price[i-1];
i++;
};
i = 1;
while (i<6)
{
cout << "Item " << i << ": " << Item[i-1] << ", Price: " << Price[i-1] << endl;
i++;
};
return 0;
}
I have the following code
for (int i = 0; i < courses; i++)
{
cout << "Please Enter Letter Grade: ";
cin >> grade1;
cout << "Please Enter Course Weighting: ";
cin >> weight1;
}
Now, lets say the loop runs 3 times and the values entered by the user for grade1 and weight1 are different each time. I want to store these different values so I can do some calculations with them. How would I proceed to do so?
Here is how use an array:
int grade[courses]; // this is an array with size of courses
double weight[courses];
for (int i = 0; i < courses; i++) {
cout << "Please Enter Letter Grade: ";
cin >> grade[i];
cout << "Please Enter Course Weighting: ";
cin >> weight[i];
}
Array is collection of data of the same type stored sequentially in computer memory. Syntax for array is as follow:
<type> <name>[<size>];
for example
int numberOfStudents[100];
is int array with maximum of 100 elements.
Hope This Helps
group grade and weight into a struct and store them in a vector.
code: (doesnt handle all potential errors)
#include <iostream>
#include <vector>
struct grade_weight
{
int grade;
int weight;
};
int main()
{
int courses = 5;
std::vector<grade_weight> result;
// potential optimization if you want
//result.reserve(courses);
for (int i = 0; i < courses; i++)
{
int grade, weight;
std::cout << "Please Enter Letter Grade: ";
std::cin >> grade;
std::cout << "Please Enter Course Weighting: ";
std::cin >> weight;
result.push_back({grade, weight});
}
std::cout << "you input\n";
for(auto& gw: result)
{
std::cout << "grade: " << gw.grade << ", weight: " << gw.weight << '\n';
}
}
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
So I have this do-while loop which is supposed to repeat asking if you want to enter info for a student if you do press y and enter info via an input() function the data is then stored in a vector. If you press q, the program is supposed to print the info contained in the vector and exit the loop.
For some reason the loop fully executes for the first and second student you enter. Then instead of repeating the loop asking if you want to enter a third student it seems to just execute the input() function. It doesn't ask you 'Enter y to continue or q to quit' and any student info you enter here does not seemed to be stored. It does this intermittently changing between executing the full loop and just the input() function. I'm wondering if anyone knows why this is happening and what I can do to fix it.
Cheers
#include <iostream>
#include <iomanip>
#include <cmath>
#include <cstring>
#include <string>
#include <vector>
using namespace std;
const int NO_OF_TEST = 4;
struct studentType
{
string studentID;
string firstName;
string lastName;
string subjectName;
string courseGrade;
int arrayMarks[4];
double avgMarks;
};
studentType input();
double calculate_avg(int marks[],int NO_OF_TEST); // returns the average mark
string calculate_grade (double avgMark); // returns the grade
void main()
{
unsigned int n=0; // no. of student
vector<studentType> vec; // vector to store student info
studentType s;
char response;
do
{
cout << "\nEnter y to continue or q to quit... ";
cin >> response;
if (response == 'y')
{
n++;
for(size_t i=0; i<n; ++i)
{
s = input();
vec.push_back(s);
}
}
else if (response == 'q')
{
for (unsigned int y=0; y<n; y++)
{
cout << "\nFirst name: " << vec[y].firstName;
cout << "\nLast name: " << vec[y].lastName;
cout << "\nStudent ID: " << vec[y].studentID;
cout << "\nSubject name: " << vec[y].subjectName;
cout << "\nAverage mark: " << vec[y].avgMarks;
cout << "\nCourse grade: " << vec[y].courseGrade << endl << endl;
}
}
}
while(response!='q');
}
studentType input()
{
studentType newStudent;
cout << "\nPlease enter student information:\n";
cout << "\nFirst Name: ";
cin >> newStudent.firstName;
cout << "\nLast Name: ";
cin >> newStudent.lastName;
cout << "\nStudent ID: ";
cin >> newStudent.studentID;
cout << "\nSubject Name: ";
cin >> newStudent.subjectName;
for (int x=0; x<NO_OF_TEST; x++)
{ cout << "\nTest " << x+1 << " mark: ";
cin >> newStudent.arrayMarks[x];
}
newStudent.avgMarks = calculate_avg(newStudent.arrayMarks,NO_OF_TEST );
newStudent.courseGrade = calculate_grade (newStudent.avgMarks);
return newStudent;
}
double calculate_avg(int marks[], int NO_OF_TEST)
{
double sum=0;
for( int i=0; i<NO_OF_TEST; i++)
{
sum = sum+ marks[i];
}
return sum/NO_OF_TEST;
}
string calculate_grade (double avgMark)
{
string grade= "";
if (avgMark<50)
{
grade = "Fail";
}
else if (avgMark<65)
{
grade = "Pass";
}
else if (avgMark<75)
{
grade = "Credit";
}
else if (avgMark<85)
{
grade = "Distinction";
}
else
{
grade = "High Distinction";
}
return grade;
}
I think this code does it:
n++;
for(size_t i=0; i<n; ++i)
{
s = input();
vec.push_back(s);
}
It asks you for two students the second time, for three student the third time, etc.
So, make it
vec.push_back(input());