Go Back   Computer Forums > General Computing > Programming
Click Here to Login
Join Computer forums Today


Reply
 
Thread Tools Search this Thread Display Modes
 
Old 07-06-2005, 06:05 PM   #1
Beta Member
 
Join Date: Jul 2005
Posts: 5
Default How do you allow a user of a program to enter words?

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 ();
}
}
__________________

Jetthreat11 is offline   Reply With Quote
Old 07-06-2005, 06:16 PM   #2
Beta Member
 
Join Date: Jul 2005
Posts: 5
Default Re: How do you allow a user of a program to enter words?

I forgot to mention that i am running on Dev-C++. Thnx.
__________________

Jetthreat11 is offline   Reply With Quote
Old 07-07-2005, 12:25 AM   #3
Daemon Poster
 
Starr's Avatar
 
Join Date: Apr 2005
Posts: 926
Send a message via AIM to Starr Send a message via MSN to Starr
Default Re: How do you allow a user of a program to enter words?

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!
__________________
AMD Athlon 64 3200+ Venice Core, MSI K8N Neo-4, 2x 512 Corsair Dual Channel, Nvidia Geforce fx 6600 256MB, 160GB HardDrive

Windows Vista Business Edition

"There are 10 types of people in the world. Those who know binary and those who don't."
Starr is offline   Reply With Quote
Old 07-07-2005, 01:22 AM   #4
Beta Member
 
Join Date: Jul 2005
Posts: 5
Default

So how would that look as part of the code? Thnx.
Jetthreat11 is offline   Reply With Quote
Old 07-07-2005, 08:12 AM   #5
Daemon Poster
 
Tulsileaf's Avatar
 
Join Date: Jul 2004
Posts: 1,249
Default Re: How do you allow a user of a program to enter words?

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.
__________________
(|141 73g ()7 (|33|/| 11143s| |_| z1|-|7 (|43s| |/|4> |_| |-|0|1
Tulsileaf is offline   Reply With Quote
Old 07-07-2005, 08:34 AM   #6
Daemon Poster
 
RewtGuy's Avatar
 
Join Date: Dec 2004
Posts: 595
Send a message via AIM to RewtGuy
Default Re: How do you allow a user of a program to enter words?

Just curious, would you want to save all that to a file or something?
__________________
Windows: A thirty-two bit extension and GUI shell to a sixteen bit patch to an eight bit operating system originally coded for a four bit microprocessor and sold by a two-bit company that can't stand one bit of competition.
RewtGuy is offline   Reply With Quote
Old 07-07-2005, 03:15 PM   #7
Beta Member
 
Join Date: Jul 2005
Posts: 5
Default

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.
Jetthreat11 is offline   Reply With Quote
Old 07-08-2005, 01:33 AM   #8
Daemon Poster
 
RewtGuy's Avatar
 
Join Date: Dec 2004
Posts: 595
Send a message via AIM to RewtGuy
Default Re: How do you allow a user of a program to enter words?

#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;
}
__________________
Windows: A thirty-two bit extension and GUI shell to a sixteen bit patch to an eight bit operating system originally coded for a four bit microprocessor and sold by a two-bit company that can't stand one bit of competition.
RewtGuy is offline   Reply With Quote
Old 07-08-2005, 01:35 AM   #9
Daemon Poster
 
RewtGuy's Avatar
 
Join Date: Dec 2004
Posts: 595
Send a message via AIM to RewtGuy
Default Re: How do you allow a user of a program to enter words?

i'll try and write a function tomorrow when i wake up to save to a specific location.
__________________
Windows: A thirty-two bit extension and GUI shell to a sixteen bit patch to an eight bit operating system originally coded for a four bit microprocessor and sold by a two-bit company that can't stand one bit of competition.
RewtGuy is offline   Reply With Quote
Old 07-09-2005, 10:11 PM   #10
Beta Member
 
Join Date: Jul 2005
Posts: 5
Default

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.
__________________

Jetthreat11 is offline   Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off



All times are GMT -5. The time now is 05:32 AM.


Powered by vBulletin® Version 3.8.8 Beta 4
Copyright ©2000 - 2017, vBulletin Solutions, Inc.
Search Engine Friendly URLs by vBSEO 3.6.0