Simple string problem

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
Last edited on
cin is going to stop after the space.
read about getline and how to use here
http://www.augustcouncil.com/~tgibson/tutorial/iotips.html
Yes, as you can see in my code I changed cin to cin.getline
I even tried changing the value to string data and then changed the call to
getline(cin,raceName1);

and it still skips.
you didn't do it for city name and I don't see where you used std::cin.ignore()

Read the section under
std::getline() can run into problems when used before std::cin >> var.
Last edited on
I read through that article and changed some things. I almost have it. I can get everything working except raceName1 is not showing up, but date will. I tried rearranging it multiple times, deleting things..its the only thing not working. Thanks for your help. I'm so close!

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
    cout<<"Please enter the name of the race."<<endl;
    
    getline(cin,raceName1);
    cin.ignore(25,'\n');
    
    cout<<"Please enter the date of the race."<<endl;
    
    getline(cin,date);
    cin.ignore(25,'\n');

    int totalseconds = hours*60*60+minutes*60+seconds;

    double secondspermile=totalseconds/miles;

    double paceminutes=secondspermile/60;

    double paceseconds=(int)secondspermile%60;
    

    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<<"Location: "<<city<<","<<state<<endl;
    
    cout<<"Race name: "<<raceName1<<endl;
   
    cout<<"Date: "<<date<<endl;


    return 0;

}


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.
Tishomingo
Please enter the abbreviated state.
MS
Please enter the name of the race.
Swinging Bridge Run
Please enter the date of the race.
April 21, 2018

Peace minutes: 11|Pace seconds: 54
Miles per hour: 5 MPH
Location: Tishomingo,MS
Race name:
Date: April 21, 2018

Last edited on
I figured it out, I didn't realize I had to put the cin.ignore before the input because I was entering string after an integer. Thank you for your help.

1
2
3
4
5
 
    cout<<"Please enter the name of the race."<<endl;
    cin.ignore(50,'\n');
    getline(cin,raceName1);
    
Topic archived. No new replies allowed.