I am very new at c++ and I am trying to get create a program where the user can type in the information that will added up to get and then they will get end up getting the total of all three numbers. This is what that code looks like:
#include <cstdlib>
#include <iostream>
#include <string>
#include <iomanip>
/*
*
*/
int main(int argc, char** argv) {
usingnamespace std;
string DriverOne; //The First race car name
string DriverTwo; // Second race car name
string DriverThree; // Third race car name
int CarNum; // Car Number
string CarColor; // Color of the car
int LapTime1; // Lap Time of the first lap
int LapTime2; // Lap time of the second lap
int LapTime3; // Lap time of the third lap
string TotalLapTime;
DriverOne = ("Billy");
DriverTwo = ("Susan");
DriverThree = ("Mike");
cout<<"Driver Name:"<< DriverOne<< endl;
// Must enter the race car number
cout <<"Enter the car number:";
cin >> CarNum;
// Must enter the race car color
cout <<"Enter the color of the car:";
cin >>CarColor;
// Must enter the lap time of the first lap
cout <<"Enter the lap time of the first lap:";
cin >>LapTime1;
// Must enter the lap time of the second lap
cout<<"Enter the lap time of the second lap:";
cin >> LapTime2;
// Must enter the lap time of the third lap
cout << "Enter the lap time of the third lap:";
cin >>LapTime3;
TotalLapTime = LapTime1 +LapTime2 +LapTime3;
cout << "Driver Name:" << DriverOne <<endl;
cout <<"The car number:" <<CarNum <<endl;
cout <<"The color of the car:" <<CarColor <<endl;
cout <<"The first lap time:" <<LapTime1 <<endl;
cout <<"The second lap time:" <<LapTime2 <<endl;
cout <<"The third lap time:" <<LapTime3 <<endl;
return 0;
}
Double posting doesn't help - it just splits potential answers across two forums.
What's the problem? You've shown a lot of code but not indicated what the issue is. Doing so will spare someone the time of reading through your code and guessing (or building/running it).
You are not doing much wrong, you just forgot to output the TotalLapTime.
Driver Name:Billy
Enter the car number:1
Enter the color of the car:red
Enter the lap time of the first lap:10
Enter the lap time of the second lap:20
Enter the lap time of the third lap:30
Driver Name:Billy
The car number:1
The color of the car:red
The first lap time:10
The second lap time:20
The third lap time:30
But how would I get it to where the program adds up the total laptimes
You're doing that at line 49. As Thomas1965 said, you're not displaying TotalLapTime.
Add after line 56:
cout << "The total lap time is: " << TotalLapTime << endl;
Line 11: usingnamespace std; belongs after your includes, not inside main.
PLEASE USE CODE TAGS (the <> formatting button) when posting code.
It makes it easier to read your code and also easier to respond to your post. http://www.cplusplus.com/articles/jEywvCM9/
Hint: You can edit your post, highlight your code and press the <> formatting button.
I fixed that line but once i did and I tried to add the other two drivers but in the output would repeat the same information from the first driver. What am i doing wrong?
You need to repeat your code for both of the other two drivers and add the variables to suit. This is a laborious process (imagine if you had 1000 drivers!).
The solution to this problem is to store the data in a set of arrays, vectors or other data structure.
#include <cstdlib>
#include <iostream>
#include <string>
#include <iomanip>
usingnamespace std; // Belongs here, not inside main
constint MAX_DRIVERS = 3;
constint MAX_LAPS = 3;
// A structure to hold information about each driver
struct Driver
{ string name;
int lap_time[MAX_LAPS]; // times for each lap for this driver
string car_color;
int car_number;
int total_time;
};
// Display info about one driver
void display_driver (const Driver & driver)
{ cout << "Driver Name:" << driver.name << endl;
cout << "The car number:" << driver.car_number << endl;
cout << "The color of the car:" << driver.car_color << endl;
for (int j=0; j<MAX_LAPS; j++)
cout << "The time for lap #" << j+1 << ": " << driver.lap_time[j] << endl;
cout << "Drivers total time = " << driver.total_time << endl;
}
// Prompt for information about each driver
void enter_driver (int i, Driver & driver)
{ cout << "Driver # " << i+1 << "name: ";
cin >> driver.name;
cout << "Enter the car number: ";
cin >> driver.car_number;
cout << "Enter the color of the car: ";
cin >> driver.car_color;
driver.total_time = 0;
// Prompt for the lap time for each lap
for (int j=0; j<MAX_LAPS; j++)
{ cout << "Enter the time of lap # " << j+1 << ": ";
cin >> driver.lap_time[j];
driver.total_time += driver.lap_time[j];
}
}
int main ()
{ Driver drivers[MAX_DRIVERS]; // Allocate array of structs
// Prompt for driver information
for (int i=0; i<MAX_DRIVERS; i++)
enter_driver (i, drivers[i]);
// Display the drivers
for (int i=0; i<MAX_DRIVERS; i++)
display_driver (drivers[i]);
system ("pause");
return 0;
}