How do you allow a user of a program to enter words?

Jetthreat11

Beta member
Messages
5
Right now i am trying to design this program that allows me to keep track of my Hot Wheels collection. Im stuck on this one part that is supposed to allow me to enter the name of the car. Im pretty sure the part where im supposed to enter the series name is wrong too. All help is appreciated. Thnx. The code reads as follows:



#include <stdlib.h>
#include <iostream>
#include <iomanip>


#define MAXNAMELENGTH 25

using namespace std;

int main()
{
int colnum;
int year;
bool yes;
int seriestotal;
int seriesloc;
char car[MAXNAMELENGTH];
char series[MAXNAMELENGTH];

cout << "\n\n\n\n Enter the collector number: ";
cin >> colnum;
cout << "\n\n Enter the name of the car: ";
cin.get (car, MAXNAMELENGTH);
cout << "\n\n Enter the year of the car: ";
cin >> year;
cout << "\n\n Is your car part of a series (1 = Yes and 0 = No): ";
cin >> yes;
if (yes >= 1) {
cout << "\n\n Enter the name of the cars series: ";
cin.get (series, MAXNAMELENGTH);
cout << "\n\n How many cars are in the series: ";
cin >> seriestotal;
cout << "\n\n What number is the car in the series: ";
cin >> seriesloc;
cout << "\n\n Here is the information you submitted: ";
cout << " " << year << " " << car << " " << seriesloc << "/" << seriestotal << " in the " << series << " series, #" << colnum << " " << "\n\n";
system ("pause");
main ();
}
else if (yes <= 0) {
cout << "\n\n Here is the information you submitted: ";
cout << " " << year << " " << car << " #" << colnum << "\n\n";
system ("pause");
main ();
}
}
 
Instead of using character arrays for the car name and series try just using a string and use 'cin.' If you do that then don't forget the string library!
 
i think it would be....

Code:
#include <string>
...
string series;
...
cin >> series;
...

i normally don't use the string library. i'd use cin.get as you did. some would say thats not the best way. i would say its a personal preference.

did your code compile and run as you wanted? i think the recursive call to main is a little wierd. why not make a seperate function to do that or how about using a while loop or a for loop that iterates for the number of entries being made.

also you have included the iomanip library but did not use it. i suggest you utilize it to format the output.
 
Just curious, would you want to save all that to a file or something?
 
RewtGuy: Yeah i would actually like to save it to a file. However I am pretty new at this and have yet to learn many simple things such as saving to a file.

Tulsileaf: As for the iomanip library, that was a mistake i did not mean to include it within the code. Actually i dont even know how to use it so....neway, the program runs until i get to the part where it asks if the car is part of a series. If i push no, it works and tells me the information that i submitted, like its supposed to. But when it goes back to the start it goes into an uncontrolled loop. However the recursive call back to main has worked for me in other simple programs very well. If i push yes at the series selection screen it imediatily goes into an uncontrolled loop and puts false information into the output stream. I also tried using the:
#include <string>

string series;

cin >> series;

But it did the same thing as before; an endless loop.
 
#include <cstdlib> // using cstdlib instead stdlib.h
#include <iostream>


#define MAXNAMELENGTH 25

using namespace std;

int main()
{

int colnum;
int year;
bool yes;
int seriestotal;
int seriesloc;
char car[MAXNAMELENGTH];
char series[MAXNAMELENGTH];


cout << "\n\n\n\n Enter the collector number: ";
cin >> colnum;

cout << "\n\n Enter the name of the car: ";
cin.get (car, MAXNAMELENGTH);

cout << "\n\n Enter the year of the car: ";
cin >> year;

cout << "\n\n Is your car part of a series (1 = Yes and 0 = No): ";
cin >> yes;

if (yes >= 1) {

cout << "\n\n Enter the name of the cars series: ";
cin.get (series, MAXNAMELENGTH);

cout << "\n\n How many cars are in the series: ";
cin >> seriestotal;

cout << "\n\n What number is the car in the series: ";
cin >> seriesloc;

cout << "\n\n Here is the information you submitted: ";
cout << " " << year << " " << car << " " << seriesloc << "/" << seriestotal << " in the " << series << " series, #" << colnum << " " << "\n\n";

system ("pause");
} // end of IF statement main (); removed to stop loop

else if (yes <= 0) {

cout << "\n\n Here is the information you submitted: ";
cout << " " << year << " " << car << " #" << colnum << "\n\n";

system ("pause");
} // end of else if statement main(); removed to stop loop
return 0;
}
 
i'll try and write a function tomorrow when i wake up to save to a specific location.
 
Thanks Rewtguy, but it still doesn't work. I copied and pasted the code you submitted and compiled it but when i ran it, it allowed me to enter the collector number and then jumped to end of the program. Similar to what it was previously doing. Thanks for trying to help. I really appreciate it. We'll get it sooner or later.
 
Back
Top Bottom