Array and Loop Trouble
Feb 9, 2016 at 3:35am UTC
Need help getting a lot of errors with my array and loop to input different information like Driver name, color, number and lap time. Please I need help ASAP!
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
#include <stdafx.h>
#include <iostream>
#include <string>
#include <iomanip>
#include <cstdlib>
using namespace std;
int main()
{
struct driverinfo
{
string drivername,
color;
int drivernumber,
laptime;
};
bool isLetter(string);
int main()
{
int index,
total = 0;
const int numdrivers = 3;
driverinfo drivers;
for (index = 0; index < numdriver; index++)
{
cout << " " << endl;
cout << "Enter the name of driver # " << (index + 1);
cout << ": " ;
if (!(getline(cin, drivers[index].drivername)))
{
isLetter(drivers[index].drivername);
}
cout << " " << endl;
cout << "Enter the color for driver # " << (index + 1);
cout << ": " ;
if (!(getline(cin, drivers[index].color)))
{
isLetter(drivers[index].color);
}
cout << "Enter the number for driver # " << (index + 1);
cout << ": " ;
if (!(cin >> drivers[index].drivernum))
{
cout << "Drivers number must be numeric characters only!\n" ;
cout << "Program terminating please start over." << endl;
system("pause" );
exit(0);
}
cout << "Please enter the laptime in MM:SS for driver # " << (index + 1);
cout << ": " ;
if (!(cin >> players[index].laptime))
{
cout << "The lap time must be in numeric characters only!\n" ;
cout << "Program terminating please start over." << endl;
system("pause" );
exit(0);
}
cin.ignore();
}
cout << "\n" ;
cout << "Here is the information for each driver: \n" ;
cout << fixed << showpoint << setprecision(2) << setw(5);
cout << "\n" ;
cout << setw(26) << "Name" << setw(16) << "color" << setw(18) << "Number" << setw(16) << "Points\n" ;
cout << "-----------------------------------------------------------" << endl;
for (index = 0; index < numdrivers; index++)
{
cout << "Driver # " << (index + 1);
cout << ": " << setw(15) << drivers[index].drivername << setw(15) << drivers[index].color << setw(15) << drivers[index].drivernum << setw(15) << drivers[index].laptime << endl;
cout << "-----------------------------------------------------------" << endl;
}
string temp;
cout << "\nPress any key then Enter \n" ;
cin >> temp;
return 0;
}
}
Feb 9, 2016 at 3:50am UTC
You can't implement a function inside another function and there can only be one function named main().
Feb 9, 2016 at 3:51am UTC
Drivers needs to be declared as an array.
Feb 9, 2016 at 4:10am UTC
I didn't notice I had a double function I have tried to compile before and copied what I had before into a new project. Also, I rearranged the bool to be before the main() but still have problems. For some reason I'm getting errors like this
no operator "[]" matches these operands.
also getting
must have class,struct,union
but here is the revised version
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
#include <stdafx.h>
#include <iostream>
#include <string>
#include <iomanip>
#include <cstdlib>
using namespace std;
struct driverinfo
{
string drivername,
color;
int drivernumber,
laptime;
};
bool isLetter(string);
int main()
{
int index,
total = 0;
const int numdriver = 3;
driverinfo drivers;
for (index = 0; index < numdriver; index++)
{
cout << " " << endl;
cout << "Enter the name of driver # " << (index + 1);
cout << ": " ;
if (!(getline(cin, drivers[index].drivername)))
{
isLetter(drivers[index].drivername);
}
cout << " " << endl;
cout << "Enter the color for driver # " << (index + 1);
cout << ": " ;
if (!(getline(cin, drivers[index].color)))
{
isLetter(drivers[index].color);
}
cout << "Enter the number for driver # " << (index + 1);
cout << ": " ;
if (!(cin >> drivers[index].drivernum))
{
cout << "Drivers number must be numeric characters only!\n" ;
cout << "Program terminating please start over." << endl;
system("pause" );
exit(0);
}
cout << "Please enter the laptime in MM:SS for driver # " << (index + 1);
cout << ": " ;
if (!(cin >> drivers[index].laptime))
{
cout << "The lap time must be in numeric characters only!\n" ;
cout << "Program terminating please start over." << endl;
system("pause" );
exit(0);
}
cin.ignore();
}
cout << "\n" ;
cout << "Here is the information for each driver: \n" ;
cout << fixed << showpoint << setprecision(2) << setw(5);
cout << "\n" ;
cout << setw(26) << "Name" << setw(16) << "color" << setw(18) << "Number" << setw(16) << "Points\n" ;
cout << "-----------------------------------------------------------" << endl;
for (index = 0; index < numdriver; index++)
{
cout << "Driver # " << (index + 1);
cout << ": " << setw(15) << drivers[index].drivername << setw(15) << drivers[index].color << setw(15) << drivers[index].drivernum << setw(15) << drivers[index].laptime << endl;
cout << "-----------------------------------------------------------" << endl;
}
string temp;
cout << "\nPress any key then Enter \n" ;
cin >> temp;
return 0;
}
Feb 9, 2016 at 4:13am UTC
Please re-read @pnoids post above.
Feb 9, 2016 at 4:39am UTC
Thank you very much I was over looking the easiest part. Thank you for the help I wouldn't of caught that at all!! I love this community thanks again!
By any chance is there a way to fix
Error (active) cannot open source file "stdafx.h"
?
Thanks again!
Feb 9, 2016 at 5:07am UTC
By any chance is there a way to fix
Error (active) cannot open source file "stdafx.h"
Sure, delete this line:
#include <stdafx.h>
Topic archived. No new replies allowed.