How do I write multiple output from a loop? - c++

I got this program to calculate the grades to its corresponding letter grade, and I got it to loop as many times as the user wants to output. However, for some reason it only writes the last known output to its text file. Can anyone tell me what I am doing wrong here?
#include <fstream>
#include <iostream>
#include <iomanip>
#include <string>
#include <cstdlib>
int weighted1 = 0;
int weighted2 = 0;
int weighted3 = 0;
int weighted4 = 0;
int weighted_average = 0;
const int MAX = 20;
int flag = 0;
int choice;
double sum = 0;
double average = 0;
char name [10];
char letter;
char file [MAX];
int num = 0;
int count = 0;
int main( )
{
//Initiate input/output stream
std::ifstream in_stream;
std::ofstream out_stream;
in_stream.open("grade.txt");
out_stream.open("finalgrade.dat");
double first, second, third, fourth;
in_stream >> first >> second >> third >> fourth >> name;
//std::cout >> " 1: " >> first >> " 2: " >> second >>
double grade = 0.0;
grade = (first + second + third + fourth)/4;
//Gives user the choice of reading student records from keyboard or file
bool menu = true;
while (menu != false)
{
std::cout << "Would you like to open as keyboard or file?" << '\n';
std::cout << "1. keyboard" << '\n';
std::cout << "2. file" << '\n';
std::cin >> choice;
switch (choice)
{
//Enter the number students the grades will enter
case 1:
std::cout << "How many students? ";
std::cin >> num;
for(count =0; count < num; count++)
{
{
std::cout << "Student's Name: ";
std::cin >> name;
}
do
{
flag = 0;
std::cout << "Please input your first exam grade and press enter: \n";
std::cin >> first;
if ((first < 0) || (first > 100))
{
std::cout << "You've entered invalid data!" << '\n';
flag = 1;
}
}while (flag == 1);
do
{
flag = 0;
std::cout << "Please input your second exam grade and press enter: \n";
std::cin >> second;
if ((second < 0) || (second > 100))
{
std::cout << "You've entered invalid data!" << '\n';
flag = 1;
}
}while (flag == 1);
do
{
flag = 0;
std::cout << "Please input your third exam grade and press enter: \n";
std::cin >> third;
if ((third < 0) || (third > 100))
{
std::cout << "You've entered invalid data!" << '\n';
flag = 1;
}
}while (flag == 1);
do
{
flag = 0;
std::cout << "Please input your final exam grade and press enter: \n";
std::cin >> fourth;
if ((fourth < 0) || (fourth > 100))
{
std::cout << "You've entered invalid data!" << '\n';
flag = 1;
}
}while (flag == 1);
//Formulas that calculate student average
grade = (first + second + third + fourth)/4;
sum = first + second + third + fourth;
average = sum/4;
//Letter grade and it's weighted averages
letter = 'A';
letter = 'B';
letter = 'C';
letter = 'D';
letter = 'F';
if(grade >= 90)
{
letter = ('A');
std::cout<<letter<<'\n';
}
else if(grade >= 80)
{
letter = ('B');
std::cout<<letter<<'\n';
}
else if(grade >= 70)
{
letter = ('C');
std::cout<<letter<<'\n';
}
else if(grade >= 60)
{
letter = ('D');
std::cout<<letter<<'\n';
}
else if (grade < 60)
{
letter = ('F');
std::cout<<letter<<'\n';
}
weighted1 = (first * .20);
weighted2 = (second * .20);
weighted3 = (third * .20);
weighted4 = (fourth * .40);
weighted_average = (weighted1 + weighted2 + weighted3 + weighted4);
//Output
std::cout << "Exam Grades: " << first << "," << second << "," << third << "," << fourth << '\n';
std::cout << "This is the average for " << name << ": " << weighted_average << '\n';
std::cout << "This is the letter grade: " << letter << '\n';
}
{
//Writing the grade into grades.txt
for(count =0; count < num; count++)
{
std::ofstream myfile;
myfile.open ("grades.txt");
myfile << "Writing this to a file: ";
myfile << name << ' ';
myfile << weighted_average << ' ';
myfile << letter << '\n';
myfile << "****";
myfile.close();
}
break;
}
//Here we open "grade.txt" to output grade to screen
case 2:
in_stream.open("grade.txt");
out_stream.open("finalgrade.dat");
letter = 'A';
letter = 'B';
letter = 'C';
letter = 'D';
letter = 'F';
if(grade >= 90)
letter = ('A');
else if(grade >= 80)
letter = ('B');
else if(grade >= 70)
letter = ('C');
else if(grade >= 60)
letter = ('D');
else if (grade < 60)
letter = ('F');
weighted1 = (first * .20);
weighted2 = (second * .20);
weighted3 = (third * .20);
weighted4 = (fourth * .40);
weighted_average = (weighted1 + weighted2 + weighted3 + weighted4);
std::cout << "Enter file name: ";
std::cin >> file;
if(file != "grade.txt")
{
std::cout << std::fixed << "The average grade for: " << name << '\n';
std::cout << "average in grade.txt is: "<< weighted_average << std::setprecision(2) << '\n';
std::cout << "and the letter grade is: " << letter << '\n';
}
else
{
return 0;
}
in_stream.close();
out_stream.close();
}
return 0;
}
}

EDIT: The more serious issue here is that you're only storing the last input. You should create an object to store all of the data for each student (e.g. a Student object), create an array of students, and loop through that array to print the information after you have all of your input. I've updated the code below to what it would look like for an array of objects.
If you don't know any object-oriented programming concepts, you could also put each piece of data (name, letter grade, average, etc.) in an array, where the 0th element in each would all represent one student, the 1st would represent another, etc. This isn't good practice; it's a much better idea to create an object to store the information about a student.
Original: You're overwriting your file instead of appending to it by opening and closing it inside every iteration of the loop.
Instead, open your file before the loop and close it after, like this:
{
//Writing the grade into grades.txt
std::ofstream myfile;
myfile.open ("grades.txt");
for(count =0; count < num; count++)
{
myfile << "Writing this to a file: ";
myfile << students[count].name << ' ';
myfile << students[count].weighted_average << ' ';
myfile << students[count].letter << '\n';
myfile << "****";
}
myfile.close();
}

if your trying to output many things i suggest you
Iterate through the loop adding your intended output to a variable, after the loop finishes output that variable
Example
var output
while(true) {
add to output
}
print output

Related

How to read and store a string value in c++ [duplicate]

This question already has answers here:
Why does std::getline() skip input after a formatted extraction?
(5 answers)
Why must type getline(cin, string) twice?
(5 answers)
Closed 9 months ago.
what's wrong
if I use the get line function only once in the character modifier function the compiler will ignore it
unless I call the function twice
why cant I use it only once?
I tried using other ways, it worked but I wanna understand this one
I'm now just writing random things so the add more details error messages go away
#include <iostream>
#include<string>
using namespace std;
class egybest
{
string link,m;
char sys, type, restart;
int s = 1, e = 1, date;
public:
string charmodifier()
{
//here
getline(cin, m);
getline(cin, m);
for (int x = 0; x <= m.size(); x++)
{
if (m[x] == ' ')
m[x] = '-';
}
return m;
}
~egybest()
{
system("cls");
cout << "do you want to restart the program? y:n;" << endl;
cin >> restart;
system("cls");
if (restart == 'y' || restart == 'Y')
egybest();
else if (restart == 'n' || restart == 'N')
{
system("exit");
}
}
egybest()
{
cout << "do you want to watch a movie or a series? 1:2;" << endl;
cin >> type;
system("cls");
if (type == '1')
linkmovie();
else if (type == '2')
series();
else
cout << "wrong input!" << endl;
}
void linkmovie()
{
cout << "enter the name of the movie:" << endl;
charmodifier();
cout << "enter the release date: " << endl;
cin >> date;
link = "start https://cape.egybest.cool/movie/" + m + "-" + to_string(date);
cout << endl;
system(link.c_str());
}
void series()
{
cout << "do you want it to open links for a particular season, particular episode or all seasons? s:e:a;"
<< endl;
cin >> sys;
system("cls");
if (sys == 'S' || sys == 's')
linkseason();
else if (sys == 'A' || sys == 'a')
linkall();
else if (sys == 'E' || sys == 'e')
linkepisode();
else
cout << "wrong input!" << endl;
}
void linkall()
{
cout << "season No." << endl;
cin >> s;
cout << "episode No." << endl;
cin >> e;
cout << "enter the name of the show:" << endl;
charmodifier();
for (int j = 1; j <= s; j++)
{
for (int i = 1; i <= e; i++)
{
link = "start https://cape.egybest.cool/episode/" + m + "-season-" + to_string(j) + "-ep-" + to_string(i);
system(link.c_str());
}
}
cout << endl;
}
void linkepisode()
{
cout << "season No." << endl;
cin >> s;
cout << "episode No." << endl;
cin >> e;
cout << "enter the name of the show:" << endl;
charmodifier();
link = "start https://cape.egybest.cool/episode/" + m + "-season-" + to_string(s) + "-ep-" + to_string(e);
cout << endl;
system(link.c_str());
}
void linkseason()
{
cout << "season No." << endl;
cin >> s;
cout << "episodes No." << endl;
cin >> e;
cout << "enter the name of the show:" << endl;
charmodifier();
for (int i = 1; i <= e; i++)
{
link = "start https://cape.egybest.cool/episode/" + m + "-season-" + to_string(s) + "-ep-" + to_string(i);
cout << endl;
system(link.c_str());
}
}
};
int main()
{
egybest egy;
return 0;
}```
The problem is that after entering an integer or a character as for example
cout << "episode No." << endl;
cin >> e;
cout << "enter the name of the show:" << endl;
charmodifier();
//...
the input buffer contains the new line character '\n' that corresponds to the pressed Enter key.
So the following call of getline reads an empty string until the new line character is encountered.
In such a case before calling getline you need to remove the new line character from the input buffer like for example
#include <limits>
//...
cout << "episode No." << endl;
cin >> e;
cout << "enter the name of the show:" << endl;
std::cin.ignore( std::numeric_limits<std::streamsize>::max(), '\n' );
charmodifier();
//...

Reading next line from file

So the program is supposed to read from the file, which has 4-5 lines of information on it. I can read the first line into the program, and process it through the various algorithms, but I'm not sure how to loop the next line into it and process it as well, again and again until the end of the file. Thank you very much for reading and all input is appreciated. Here is the entire program, with the text read in from file at the bottom.
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main()
{
ifstream inputFile;
ofstream invoicefile;
string name, author, isbn, customerid, filename, fictionoutput, genreoutput;
char booktype, genre;
bool fictionvalue;
double initial_total, tax_price, subtotal, totalprice, price;
int fee, quantity;
const double tax(0.07);
cout << "Enter name of file.\n";
cin >> filename;
cout << "Opening file \n";
inputFile.open(filename);
if (inputFile.is_open()) {
inputFile >> customerid >> name >> author >> isbn >> price >> quantity >> booktype >> genre;
//QUANTITY FEE CODING BLOCK
if (quantity > 50) {
fee = 50;
}
else if (quantity >= 15 && quantity <= 19) {
fee = 40;
}
else if (quantity >= 10 && quantity <= 14) {
fee = 30;
}
else if (quantity >= 5 && quantity <= 10) {
fee = 20;
}
else if (quantity < 5) {
fee = 10;
}
//BOOKTYPE CODING BLOCK (FICTION or NON-F)
if (booktype == 'F') {
fictionvalue = true;
}
else if (booktype == 'N') {
fictionvalue = false;
}
else {
cout << "INVALID";
}
//BOOKTYPE INTO STRING OUTPUT
if (fictionvalue = true) {
fictionoutput = "Fiction";
}
else if (fictionvalue = false) {
fictionoutput = "Non-Fiction";
}
//GENRE TYPE INTO STRING OUTPUT
if (genre == 'R') {
genreoutput = "Romance";
}
else if (genre == 'D') {
genreoutput = "Drama";
}
else if (genre = 'M') {
genreoutput = 'M';
}
else {
cout << "Invalid entry\n";
}
//NO FEE EXCEPTION
if (booktype == 'N' && genre == 'R') {
fee = 0;
}
//CALCULATION OF PRICE + TAX CODING BLOCK
initial_total = (price*quantity);
tax_price = (initial_total * tax);
subtotal = (initial_total + tax_price);
totalprice = (subtotal + fee);
//OUTPUT TO FILE/CONSOLE CODING BLOCK
cout << "-----------------------------------------" << endl;
cout << "Order Invoice" << endl;
cout << "Customer ID: " << customerid << endl;
cout << name << " " << author << " " << fictionoutput << " " << genreoutput << " " << quantity << "#" << price << "Subtotal: " << endl; //add subtotal price
//cout << "Total book sales: " <<
cout << "Tax: " << tax_price << endl;
cout << "Subtotal: " << subtotal << endl;
cout << "Fee: " << fee << endl;
cout << "Total Price: " << totalprice << endl;
cout << "-----------------------------------------" << endl;
system("pause");
}
}
TEXT SAMPLE
1234 Dog_Strategy Henry_Moreno 3-598-21500-2 12.99 5 N M
6789 Companion_Kicked_Me_Out Lorraine_Johnson 3-598-21599-1 24.99 3 F R
3444 Mime_On_My Journey Kristy_Wahl 3-699-21500-8 6.75 10 N D
4455 Damaged_By_The_Joke Henry_Christopher 3-598-21500-2 12.99 4 N R
Maybe try using a loop like this:
// Create an empty string
std::string line;
// Start a loop that will get a line from the file and input it in our string
// this loop will keep going until the getline fails, i.e. end of file.
while (std::getline(fileName, line))
{
CODE
}
you can put a while loop that will run until the program saw the end of file
while(!EOF)
{your code here}
and always dont forget to close the file you opened

Saving all Inputs entered for display on cout - C++

I have looked in the forum but can't seem to find anything specific to what I need.
I am writing a program that asks the user to input a number of students. Depending on the number of students they enter they will then have to enter the students name and a series of 10 grades, or pressing 999 to cancel. The program will later have to display all students entered with their average grade. What I have now just overrides the previous inputs and displays the last one entered.
This is what I have so far:
#include <iostream>
#include <iomanip>
#include <string>
#include <fstream>
int main() {
std::string teacherName = "";
std::string classDesignation ="";
int numStudents = 0;
std::string studentName = "";
double grade[10];
double averageGrade = 0.00;
char letterGrade;
std::cout << "Enter the teacher's name: ";
getline(std::cin, teacherName);
std::cout << "Enter the class designation: ";
getline(std::cin, classDesignation);
std::cout << "Enter the number of students ( 1 or more ): ";
std::cin >> numStudents;
std::cin.ignore();
for (int x = 0; x <= numStudents - 1; x++) {
std::cout << "Enter the student's name: ";
getline(std::cin, studentName);
for (int i = 0; i <= 9; i++) {
std::cout << "Enter grade from 0 - 100 or 999 to stop: ";
std::cin >> grade[i];
if (grade[i] == 999){
break;
}
averageGrade += grade[i];
if (averageGrade <= 59){
letterGrade = 'F';
}
if (averageGrade >= 60 || averageGrade <= 69){
letterGrade = 'D';
}
if (averageGrade >= 70 || averageGrade <= 79){
letterGrade = 'C';
}
if (averageGrade >= 80 || averageGrade <= 89){
letterGrade = 'B';
}
if (averageGrade > 90){
letterGrade = 'A';
}
}
}
std::cout << "Teacher: " << teacherName << std::endl;
std::cout << "Class: " << classDesignation << std::endl;
std::cout << "Student Name: " << studentName;
std::cout << std::setw(19) << "Average: " << averageGrade;
std::cout << " Grade: " << letterGrade << std::endl;
std::cout << "Student count: " << numStudents << std::endl;
std::cout << "Student average: " << std::endl;
std::cout << "A's: " << std::endl;
std::cout << "B's: " << std::endl;
std::cout << "C's: " << std::endl;
std::cout << "D's: " << std::endl;
std::cout << "F's: " << std::endl;
return 0;
}
Any tips?
Thank you!
#Hansel, here's what I think will work. We'll change this line to get what we want:
averageGrade += grade[i];
To:
//to declarations:
double averageGradeSum = 0.00;
//then change the line mentioned before to:
averageGradeSum += grade[i];
Then:
//if for some reason you're indexing starts at 1
averageGrade = averageGradeSum/i;
//if indexing starts at 0 like usual C++ use:
averageGrade = averageGradeSum/(i+1);
I'm not going to test this. It should make sense and if I screwed up the syntax it should be an easy google adventure to fix. Enjoy :)
From what I see, you can store them in associative vectors, one for the student name, and one for the average grade. You can also have a doubly linked list of student nodes which would look something like this:
struct student
{
std::string student_name;
int ave_grade;
// Head points to the previous student in the array.
// Tail points to the next student in the array.
student *head;
student *tail;
}
Declare a struct that everything hangs from to prevent memory leaks: student list_head; and each time you have an input, add a new node to the list.

Reading numbers from text and outputting average

So I am working to read text from a file (line by line) and output the ID, average of 4 grades following the ID, and the letter grade. So the letter grade for any average grade of 50 or above is S, anything below 50 is a U, and 2 excused classes results in the letter grade I. (No S or U if more than or equal to 2 excused).
So lets say file has the numbers:
42 50 51 57 52
48 90 -1 60 -1
40 46 -1 59 45
47 50 -1 49 50
The output should look something like this:
ID=42 Avg=52.5 Grade=S
ID=48 Excused=2 Grade=I
ID=40 Avg=50.0 Grade=S
ID=47 Avg=49.7 Grade=U
Number of Grades of Type
S U I
2 1 1
This is the output I am receiving from my code
It is reading all the numbers, but i need it to read the first number as ID and following 4 numbers as grade.
#include <fstream>
#include <iostream>
#include <string>
using namespace std;
int main()
{
ifstream in;
ofstream results;
string filename;
char grade;
int S = 0, U = 0, I = 0, i = 0, ID, excused = 0, avg;
double allscores = 0;
cout << "Enter the name of the file that has the scores of students: " << endl;
cin >> filename;
cout << "Enter the number of scores for each student: " << endl;
cin >> ID;
in.open(filename);
results.open("results.txt");
if (in)
{
while (in >> ID)
{
int first = 0
for (i = 0; i<=4; i++)
{
if (first == -1)
excused++;
else
allscores += first;
}
if (first > 4)
{
avg = allscores / (4 - excused);
if (avg >= 50.0)
{
grade = 'S';
S++;
cout << "ID=" << ID << " Avg=" << avg << " Grade =" << grade << endl;
}
else
{
grade = 'U';
U++;
cout << "ID=" << ID << " Avg=" << avg << " Grade =" << grade << endl;
}
}
else
{
grade = 'I';
I++;
cout << "ID=" << ID << " Excused=" << excused << " Grade =" << grade << endl;
}
}
}
else
{
cout << "Couldn't open file\n";
}
cout << "Number of Grades of Type" << endl;
cout << "S " << "U " << "I" << endl;
cout << S << " " << U << " " << I << endl;
in.close();
results.close();
system("pause");
return 0;
}
Your variable int first=0 is not being assigned any value other than 0 in your code so the statement if(first>4) will always be false and result the output your are getting.
You can do something like this:
while (in >> ID)//this is supposed to get the ID of each student, in order for that to happen you need to read all the 4 scores inside this loop before coming here and reading the next ID,otherwise everything will read as an ID
{
int first = 0
excused=0;//you need to reset excused on every iteration
allscores=0;//you need to reset allscore on every iteration
for (i = 0; i<4; i++)
{//here you need to get all the 4 scores
in>>first;//get each score in first
if (first == -1)
excused++;
else
allscores += first;
}
if(excused<2)//instead of if(first>4)
...//do your calculation for average score
else
...//set the grade to 'I'...
}
Here is my final solution:
#include <fstream>
#include <iostream>
#include <string>
using namespace std;
int main()
{
ifstream in;
ofstream results;
string filename;
char grade;
int S = 0, U = 0, I = 0, i = 0, ID, excused = 0, avg;
double allscores = 0;
cout << "Enter the name of the file that has the scores of students: " << endl;
cin >> filename;
cout << "Enter the number of scores for each student: " << endl;
cin >> ID;
in.open(filename);
results.open("results.txt");
if (in)
{
while (in >> ID)
{
int first = 0;
excused = 0;
allscores = 0;
for (i = 0; i < 4; i++)
{
in >> first;
if (first == -1)
excused++;
else
allscores += first;
}
if (excused < 2)
{
avg = allscores / (4 - excused);
if (avg >= 50.0)
{
grade = 'S';
S++;
results << "ID=" << ID << " Avg=" << avg << " Grade =" << grade << endl;
}
else
{
grade = 'U';
U++;
results << "ID=" << ID << " Avg=" << avg << " Grade =" << grade << endl;
}
}
else
{
grade = 'I';
I++;
results << "ID=" << ID << " Excused=" << excused << " Grade =" << grade << endl;
}
}
}
else
{
cout << "Couldn't open file\n";
}
results << "Number of Grades of Type" << endl;
results << "S " << "U " << "I" << endl;
results << S << " " << U << " " << I << endl;
in.close();
results.close();
system("pause");
return 0;
}
After the code I have it output to a file named "results".
Thanks for the help. I guess my while loop was the biggest mistake.
Especially not adding in the in >> first portion.

Cin loop never terminating

I'm having trouble getting my cin loop to terminate in my program. My program uses Linux redirection to read in input from the file hw07data, the data file look like this:
100 20 50 100 40 -1
A34F 90 15 50 99 32 -1
N12O 80 15 34 90 22 -1
The first portion is the total points for the class, the next lines are the students ID number followed by their scores, all terminated by -1.
My issue: my while loop never terminates when i run the command ./a.out < hw07data, could anyone look over my code and give me some hints? I dont want the answer, as this is homework, i just need some guidance. Thanks!!
#include <iostream>
#include <iomanip>
using namespace std;
const int SENTINEL = -1; //signal to end part of file
const int LTAB = 8; //left tab
const int RTAB = 13; //right tab
int main()
{
cout << "Grant Mercer Assignment 7 Section 1002\n\n";
cout << setprecision(2) << fixed << showpoint;
cout << left << setw(LTAB) << "ID CODE" <<
right << setw(RTAB) << "POINTS" <<
setw(RTAB) << "PCT" << setw(RTAB) <<
"GRADE" << endl;
double Percentage, //holds students percentage
AvgPercentage;
int Earnedpoints, //earned points for specific student
Totalpoints, //total points possible for all students
AvgPoints, //average points for all students
NumClass; //counts the number of students
Totalpoints = Earnedpoints = //set all vals equal to zero
AvgPoints = AvgPercentage = Percentage = NumClass = 0;
//first and last char for studentID
char Fchar,Lchar, Num1, Num2, Grade;
int TmpVal = 0; //temporary value
cin >> TmpVal;
while(TmpVal != -1) //reading in TOTAL POINTS
{
Totalpoints += TmpVal; //add scores onto each other
cin >> TmpVal; //read in next value
}
while(cin) //WHILE LOOP ISSUE HERE!
{
//read in student initials
cin >> Fchar >> Num1 >> Num2 >> Lchar >> TmpVal;
while(TmpVal != -1)
{
Earnedpoints += TmpVal; //read in points earned
cin >> TmpVal;
}
//calculate percentage
Percentage = ((double)Earnedpoints / Totalpoints) * 100;
AvgPercentage += Percentage;
NumClass++;
if(Percentage >= 90) //determine grade for student
Grade = 'A';
else if(Percentage >= 80 && Percentage < 90)
Grade = 'B';
else if(Percentage >= 70 && Percentage < 80)
Grade = 'C';
else if(Percentage >= 60 && Percentage < 70)
Grade = 'D';
else if(Percentage < 60)
Grade = 'F';
//display information on student
cout << left << Fchar << Num1 << Num2 << setw(LTAB) << Lchar << right << setw(RTAB-3) << Earnedpoints <<
setw(RTAB) << Percentage << setw(RTAB) << Grade << endl;
TmpVal = Earnedpoints = 0;
}
AvgPercentage /= NumClass;
cout << endl << left << setw(LTAB+20) << "Class size: " << right << setw(RTAB) << NumClass << endl;
cout << left << setw(LTAB+20) << "Total points possible: " << right << setw(RTAB) << Totalpoints << endl;
cout << left << setw(LTAB+20) << "Average point total: " << right << setw(RTAB) << AvgPoints << endl;
cout << left << setw(LTAB+20) << "Average percentage: " << right << setw(RTAB) << AvgPercentage << endl;
}
The output continues to ask for new input.
You may find answer there How to read until EOF from cin in C++
So you can read cin line by line using getline and parse the resulting lines, like that:
#include <iostream>
#include <sstream>
#include <string>
int main() {
int a, b;
std::string line;
while (std::getline(std::cin, line)) {
std::stringstream stream(line);
stream >> a >> b;
std::cout << "a: " << a << " - b: " << b << std::endl;
}
return 0;
}
EDIT: Do not forget to check parsing results and stream state for any failure!
You should always check that you input was successful:
if (std::cin >> TmpVal) {
// do simething with the read value
}
else {
// deal with failed input
}
In case of a failure you night want to check eof() inducating that the failure was due to having reached the end of the input.
To deal with errors, have a look at std::istream::clear() and std::istream::ignore().