Hi. I am following along in my book. It has a program written similar to the one I have written here. The book states that cin.width should limit the user to entering the specified amount of characters, but it seems to not do anything when I try it. Here is my program.
// This program analyzes data to determine how the runners placed in a race.
#include <iostream>
#include <iomanip>
#include <string>
usingnamespace std;
int main(){
char name1[5], name2[5], name3[5];
float time1, time2, time3;
int place1, place2, place3 = 0;
cout << setiosflags(ios::showpoint | ios::fixed) << setprecision(2);
cout << "Enter the name of the first runner." << endl;
cin.width(6); // This is supposed to set a limit on the characters entered.
cin >> name1;
cout << endl;
cout << "Enter the name of the second runner." << endl;
cin >> name2;
cout << endl;
cout << "Enter the name of the third runner." << endl;
cin >> name3;
cout << endl;
cout << "Enter the first runner's finish time in seconds." << endl;
cin >> time1;
cout << endl;
cout << "Enter the second runner's finish time in seconds." << endl;
cin >> time2;
cout << endl;
cout << "Enter the third runner's finish time in seconds." << endl;
cin >> time3;
cout << endl;
if (time1 > 0 && time2 > 0 && time3 > 0){
if (time1 == time2 == time3)
cout << "All three have tied!" << endl;
elseif (time1 <= time2 && time1 <= time3 && time2 <= time3){
cout << "First Place:" << name1 << endl;
cout << "Second Place:" << name2 << endl;
cout << "Third Place:" << name3 << endl;}
elseif (time1 <= time2 && time1 <= time3 && time3 <= time2){
cout << "First Place:" << name1 << endl;
cout << "Second Place:" << name3 << endl;
cout << "Third Place:" << name2 << endl;}
elseif (time2 <= time1 && time2 <= time3 && time1 <= time3){
cout << "First Place:" << name2 << endl;
cout << "Second Place:" << name1 << endl;
cout << "Third Place:" << name3 << endl;}
elseif (time2 <= time1 && time2 <= time3 && time3 <= time1){
cout << "First Place:" << name2 << endl;
cout << "Second Place:" << name3 << endl;
cout << "Third Place:" << name1 << endl;}
elseif (time3 <= time1 && time3 <= time2 && time1 <= time2){
cout << "First Place:" << name3 << endl;
cout << "Second Place:" << name1 << endl;
cout << "Third Place:" << name2 << endl;}
elseif (time3 <= time1 && time3 <= time2 && time2 <= time1){
cout << "First Place:" << name3 << endl;
cout << "Second Place:" << name2 << endl;
cout << "Third Place:" << name1 << endl;}
}
else
cout << "Invalid time entry." << endl;
return 0;
}
Ideally the cin.width should allow the program to accept the name of the first runner without skipping the second runner because the first entry was too long.
#include <iostream>
#include <iomanip>
#include <string>
usingnamespace std;
int main(){
string name1;
string name2;
cout << "Enter the name of the first runner." << endl;
cin.width(6); // This is supposed to set a limit on the characters entered.
cin >> name1;
cout << "name1:" << name1 << endl;
cin >> name2;
cout << "name2:" << name2 << endl;
system("pause");
return 0;
}
Enter the name of the first runner.
pietjeklaas
name1:pietje
name2:klaas
the data is not added until you push enter. cin reads 6 bytes and every cin after that reads ALL remainder bytes from the unput buffer. you need to set the width again if you want to read 6 bytes again.
Is there a simple way to make the entries not overlap? So name1 gets only 6 characters, but the rest of the characters from name1 just get dropped and I can still ask the user to input name2?
Oh, never mind. Stupid question. haha.
I don't know why the book asked for something complicated out of this when I could just fix everything in the declaration.
with just:
char name[7]
and nothing else.
... Actually that doesn't work either, and I need to sleep.
cout << "Enter the name of the first runner." << endl;
char name1[5];
cin.width(5);
cin >> name1;
cin.ignore(99, '\n');
cout << endl;
cout << "Enter the name of the second runner." << endl;
char name2[5];
cin.width(5);
cin >> name2;
cin.ignore(99, '\n');
cout << endl;
cout << "Enter the name of the third runner." << endl;
char name3[5];
cin.width(5);
cin >> name3;
cin.ignore(99, '\n');
cout << endl;
Though my book doesn't say anything about cin.ignore at all.