This is a homework assignment, but I have almost finished it completely. My problem is that we have to change his code to where the user inputs data. I did that and everything works except when I have to enter string data with spaces in it. It skips and writes the wrong input to the column. I know this may seem so simple, but this is my first year and I have tried changing it to char array, doing getline...its not working correctly. I need this solution for the date and the name of the race.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122
|
#include <iostream> //to use cout and cin and endl// allows using cout without std::cout
#include <string> // to use string data type
#include <cstring> //to use strlen, strcmp, strcpy
#include <cmath> //for pow function,sqrt,abs
#include <iomanip> // for set precision, setw,
#include <fstream> //for file input
#include <cassert> // to use assert to disable place before #define NDEBUG
#include <cstdlib>//for random numbers, exit function
#include <ctime>//time function used for rand seed
#include <cctype> // for toupper, tolower
#include <algorithm>
#include <locale.h>
#include <stdio.h>
#include<bits/stdc++.h>
using namespace std;
/*
*
*/
int main(int argc, char** argv) {
int hours;
int minutes;
int seconds;
double miles;
char raceName1[100];
string city;
string state;
string date;
cout<<fixed<<setprecision(0);
cout<<"Please enter the amount of miles."<<endl;
cin>>miles;
cout<<"Please enter the number of hours."<<endl;
cin>>hours;
cout<<"Please enter the number of minutes."<<endl;
cin>>minutes;
cout<<"Please enter the number of seconds."<<endl;
cin>>seconds;
cout<<"Please enter the city."<<endl;
cin>>city;
cout<<"Please enter the abbreviated state."<<endl;
cin>>state;
cout<<"Please enter the name of the race."<<endl;
cin.getline(raceName1,sizeof(raceName1)); //tried this for whitespaces
cout<<endl;
cout<<"Please enter the date of the race."<<endl;
cin>>date; //I know this is incorrect, but its just a placeholder
int totalseconds = hours*60*60+minutes*60+seconds; //calculating data
double secondspermile=totalseconds/miles;
double paceminutes=secondspermile/60;
double paceseconds=(int)secondspermile%60;
cout<<endl;
cout<<"Peace minutes: "<<(int)paceminutes<<"|"<<"Pace seconds: "<<paceseconds<<endl;
double mph = miles/((hours+((double)minutes/60))+(double)seconds/3600);
cout<<"Miles per hour: "<<mph<<" MPH"<<endl;
cout<<"Name of Race: "<<raceName1<<endl;
cout<<"Location: "<<city<<","<<state<<endl;
cout<<"Date: "<<date<<endl;
return 0;
}
|
Output:
Please enter the amount of miles.
13.1
Please enter the number of hours.
2
Please enter the number of minutes.
36
Please enter the number of seconds.
6
Please enter the city.
tishomongo
Please enter the abbreviated state.
MS
Please enter the name of the race. //problem here, it's skipping name.
Please enter the date of the race.
swinging bridge run
Peace minutes: 11|Pace seconds: 54
Miles per hour: 5 MPH
Name of Race:
Location: tishomongo,MS
Date: swinging