-
May 13th, 2010, 01:23 PM
#1
Last Question
OK so I went a different direction a little bit. The last part of my program takes the name of the user and a fact he/she would like to see added to the program. The program itself copiles fine but the very last part is to show the user what the fact he/she wanted to add. I declared the variable an array of char with up to 200 character but it is only showing the first letter I enter
Code:
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
int main()
{
ofstream outFile;
string firstName, lastName;
char newFact[200];
cin.get(newFact, 200);
char category;//declared variables
cout << "Welcome to the TRIVIA TRAINER! This application will help you prepare" << endl;//game description
cout << "for trivia. Choose one of the categories and a facts about your choosen" <<endl;
cout << "category will appear on the screen." << endl;
cout <<"Please choose a Trivia Trainer category." << endl;
cout <<"A: The Universe" << endl;
cout <<"B: U.S. History" << endl;
cout <<"C: Video Games" << endl;
cout <<"D: Movies" << endl;
cout <<"E: Sports" << endl;
cin >> category;
cout <<"You choose to learn about category: " << category << endl;
switch (category)//switch statment
{
case 'A' :
cout << "In the total universe, it is estimated that there are 10^22 stars." << endl;
cout << "It is estimated that there are 50 thousand million galaxies in the universe." << endl;
cout << "The telescope was invented in 1608 by Hans Lippershey." << endl;
cout << "The earth rotates the sun at 30 kilometers per second." << endl;
cout << "Lightyears are a measure of distance and not time. light travels about 9,500,000,000,000 kilometers in one lightyear."<< endl;
cout << "since there are no air-waves in space sound does not carry therefore you couldn't hear anything." << endl;
cout << "25% of the universe is dark matter,70% is dark energy making only 5% visible to us." << endl;
break;
case 'B':
cout << "Christopher Columbus was arrested and returned to spain because of his cruel and barbaric attitude towards natives." <<endl;
cout << "John Hanson not George Washington was the first US President." <<endl;
cout << "The first US drug company Bayer invinted Heorin as a pain killer." <<endl;
cout << "At Andrew Jacksons funeral in 1845 his pet parrot had to be removed because it was swearing."<<endl;
cout << "Christmas did not become a national holiday untill 1890."<<endl;
cout << "During the Civil War 22 black soldiers were awarded the Medal Of Honor."<<endl;
cout << "In 1945 Grand Rapids, Michigan became the first city to add flouride to its water supply."<<endl;
cout << "The first American College was Harvard founded in 1636." <<endl;
break;
case 'C':
cout << "American Online was originally designed for users to download games to there Atari 2600 via phone lines." << endl;
cout << "The PSone was first created as a CD add-on for the SNES but because of licensing issues is was developed as its own console." << endl;
cout << "During the 1975 Christmas season PONG was the highest selling game." << endl;
cout << "Super Mario World took 29,000 hours to program." << endl;
cout << "Popular Science named Sega's Dreamcast one of the most innovative products of 1999." << endl;
cout << "The original name for the XBOX console was DirectXbox because of the DirectX developers who crated it" << endl;
cout << "68% of American housholds play console or computer games." << endl;
cout << "40% of all game players are women." << endl;
break;
case 'D':
cout << "Nicholas Cage's real name is Nicholas Coppola." << endl;
cout << "Paul Newman was originally cast to play Cpt Quint in Jaws." << endl;
cout << "During Watergate, Paul Newman was considered a celebrity enemy by Richard Nixon. " << endl;
cout << "Jack Nicholson has not given a talk show interview since 1971." << endl;
cout << "Kevin Bacon has had at least a supporting role in 27% of all American made movies since 1982." << endl;
cout << "Sly Stallone's first role was in an adult film during the 70's for which he was paid 200 dollars." << endl;
cout << "The Dark Knight is the first Batman film not to have Batman in the title." << endl;
cout << "Because of his acting background, Ronald Reagen is the only US president to wear a Nazi Uniform." << endl;
cout << "Sean Connery wore a toupee in every Bond he starred in." << endl;
break;
case 'E':
cout << "The cities of Boston and Detroit have the oldest stadiums in major league baseball." <<endl;
cout << "Innovating the game an MIT instructor added dimples to basbeball bats reducing the drag." <<endl;
cout << "The NBA's coach of the year award is named after coach Red Auerbach." <<endl;
cout << "The most common nickname for major league pitchers is Lefty." <<endl;
cout << "Hank Aaron hit 20 or more homeruns in all 20 seasons played." <<endl;
cout << "Boxing is also known as pugilism and prizefighting." <<endl;
cout << "Rugby was introduced in the US in 1875 but faded as football was introduced." <<endl;
cout << "On a football field there are 19 yard lines that divide the field into 5 yd segments." <<endl;
cout << "*** Footballs weigh between 14 and 15 oz." <<endl;
cout << "In 1936 a game of cricket was disrupted when a player struck a sparrow with the ball. The bird is on display at the Lord Cricket Ground." <<endl;
break;
default:
cout << "ERROR INVALID SELECTION, PLEASE CHOOSE A CATEGORY LISTED." << endl;//error mesage
}
cout << "Please enter your first and last name." << endl;
cin >> firstName >> lastName;
outFile.open("nameFact.txt");
outFile << firstName << lastName << endl;
cout << "Please enter a fact you would like to see added to the program." << endl;
cin >> newFact;
cout << "The fact you entered is: " << newFact << endl;
outFile << newFact << endl;
outFile.close();
return 0;
}
Now while writing this I read about the cin.get function so I added it to my declarations because I found out the character arrays only read untill it find a blank space. No when I start the program It still does not show the entire fact. Aslo when I clicked start w/o debugging nothing showed on the screen untill i pressed enter.(edit) When I Choose start w/o debugging and press enter it bring me to the exit line of the program but if I enter A it lets me run the program with the problems stated above.
-
May 13th, 2010, 01:49 PM
#2
Re: Last Question
So i just figured out that it supposed to go here
Code:
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
int main()
{
ofstream outFile;
string firstName, lastName;
char newFact[200];
char category;//declared variables
cout << "Welcome to the TRIVIA TRAINER! This application will help you prepare" << endl;//game description
cout << "for trivia. Choose one of the categories and a facts about your choosen" <<endl;
cout << "category will appear on the screen." << endl;
cout <<"Please choose a Trivia Trainer category." << endl;
cout <<"A: The Universe" << endl;
cout <<"B: U.S. History" << endl;
cout <<"C: Video Games" << endl;
cout <<"D: Movies" << endl;
cout <<"E: Sports" << endl;
cin >> category;
cout <<"You choose to learn about category: " << category << endl;
switch (category)//switch statment
{
case 'A' :
cout << "In the total universe, it is estimated that there are 10^22 stars." << endl;
cout << "It is estimated that there are 50 thousand million galaxies in the universe." << endl;
cout << "The telescope was invented in 1608 by Hans Lippershey." << endl;
cout << "The earth rotates the sun at 30 kilometers per second." << endl;
cout << "Lightyears are a measure of distance and not time. light travels about 9,500,000,000,000 kilometers in one lightyear."<< endl;
cout << "since there are no air-waves in space sound does not carry therefore you couldn't hear anything." << endl;
cout << "25% of the universe is dark matter,70% is dark energy making only 5% visible to us." << endl;
break;
case 'B':
cout << "Christopher Columbus was arrested and returned to spain because of his cruel and barbaric attitude towards natives." <<endl;
cout << "John Hanson not George Washington was the first US President." <<endl;
cout << "The first US drug company Bayer invinted Heorin as a pain killer." <<endl;
cout << "At Andrew Jacksons funeral in 1845 his pet parrot had to be removed because it was swearing."<<endl;
cout << "Christmas did not become a national holiday untill 1890."<<endl;
cout << "During the Civil War 22 black soldiers were awarded the Medal Of Honor."<<endl;
cout << "In 1945 Grand Rapids, Michigan became the first city to add flouride to its water supply."<<endl;
cout << "The first American College was Harvard founded in 1636." <<endl;
break;
case 'C':
cout << "American Online was originally designed for users to download games to there Atari 2600 via phone lines." << endl;
cout << "The PSone was first created as a CD add-on for the SNES but because of licensing issues is was developed as its own console." << endl;
cout << "During the 1975 Christmas season PONG was the highest selling game." << endl;
cout << "Super Mario World took 29,000 hours to program." << endl;
cout << "Popular Science named Sega's Dreamcast one of the most innovative products of 1999." << endl;
cout << "The original name for the XBOX console was DirectXbox because of the DirectX developers who crated it" << endl;
cout << "68% of American housholds play console or computer games." << endl;
cout << "40% of all game players are women." << endl;
break;
case 'D':
cout << "Nicholas Cage's real name is Nicholas Coppola." << endl;
cout << "Paul Newman was originally cast to play Cpt Quint in Jaws." << endl;
cout << "During Watergate, Paul Newman was considered a celebrity enemy by Richard Nixon. " << endl;
cout << "Jack Nicholson has not given a talk show interview since 1971." << endl;
cout << "Kevin Bacon has had at least a supporting role in 27% of all American made movies since 1982." << endl;
cout << "Sly Stallone's first role was in an adult film during the 70's for which he was paid 200 dollars." << endl;
cout << "The Dark Knight is the first Batman film not to have Batman in the title." << endl;
cout << "Because of his acting background, Ronald Reagen is the only US president to wear a Nazi Uniform." << endl;
cout << "Sean Connery wore a toupee in every Bond he starred in." << endl;
break;
case 'E':
cout << "The cities of Boston and Detroit have the oldest stadiums in major league baseball." <<endl;
cout << "Innovating the game an MIT instructor added dimples to basbeball bats reducing the drag." <<endl;
cout << "The NBA's coach of the year award is named after coach Red Auerbach." <<endl;
cout << "The most common nickname for major league pitchers is Lefty." <<endl;
cout << "Hank Aaron hit 20 or more homeruns in all 20 seasons played." <<endl;
cout << "Boxing is also known as pugilism and prizefighting." <<endl;
cout << "Rugby was introduced in the US in 1875 but faded as football was introduced." <<endl;
cout << "On a football field there are 19 yard lines that divide the field into 5 yd segments." <<endl;
cout << "*** Footballs weigh between 14 and 15 oz." <<endl;
cout << "In 1936 a game of cricket was disrupted when a player struck a sparrow with the ball. The bird is on display at the Lord Cricket Ground." <<endl;
break;
default:
cout << "ERROR INVALID SELECTION, PLEASE CHOOSE A CATEGORY LISTED." << endl;//error mesage
}
cout << "Please enter your first and last name." << endl;
cin >> firstName >> lastName;
outFile.open("nameFact.txt");
outFile << firstName << lastName << endl;
cout << "Please enter a fact you would like to se added to the program." << endl;
cin.get(newFact, 200);
cout << "The fact you entered is: " << newFact << endl;
outFile << newFact << endl;
outFile.close();
return 0;
}
And that took care of the wied problem of it not showing up but now, when i run the program it skips over the cin.get statment all together.
-
May 13th, 2010, 02:17 PM
#3
Re: Last Question
Code:
string firstName, lastName;
char newFact[200];
Why would you use string one place, but char* another?
Change it to:
Code:
string firstName, lastName;
string newFact;
-
May 13th, 2010, 02:31 PM
#4
Re: Last Question
While reading my text book I read that "The length of the input C-string must be less than or equal to 30 characters" and if I input a fact that is 75 characters it wont work? right?
-
May 13th, 2010, 02:34 PM
#5
Re: Last Question
I tried using
string newFact;
and am still getting the same problem where I input the new fact and it only outputs the first character.
-
May 13th, 2010, 02:47 PM
#6
Re: Last Question
try:
Code:
cin >> firstName >> lastName;
cin.ignore( numeric_limits<streamsize>::max(), '\n' );
getline(cin, newFact);
-
May 13th, 2010, 03:02 PM
#7
Re: Last Question
Code:
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
int main()
{
ofstream outFile;
string firstName, lastName;
string newFact;
char category;//declared variables
cout << "Welcome to the TRIVIA TRAINER! This application will help you prepare" << endl;//game description
cout << "for trivia. Choose one of the categories and a facts about your choosen" <<endl;
cout << "category will appear on the screen." << endl;
cout <<"Please choose a Trivia Trainer category." << endl;
cout <<"A: The Universe" << endl;
cout <<"B: U.S. History" << endl;
cout <<"C: Video Games" << endl;
cout <<"D: Movies" << endl;
cout <<"E: Sports" << endl;
cin >> category;
cout <<"You choose to learn about category: " << category << endl;
switch (category)//switch statment
{
case 'A' :
cout << "In the total universe, it is estimated that there are 10^22 stars." << endl;
cout << "It is estimated that there are 50 thousand million galaxies in the universe." << endl;
cout << "The telescope was invented in 1608 by Hans Lippershey." << endl;
cout << "The earth rotates the sun at 30 kilometers per second." << endl;
cout << "Lightyears are a measure of distance and not time. light travels about 9,500,000,000,000 kilometers in one lightyear."<< endl;
cout << "since there are no air-waves in space sound does not carry therefore you couldn't hear anything." << endl;
cout << "25% of the universe is dark matter,70% is dark energy making only 5% visible to us." << endl;
break;
case 'B':
cout << "Christopher Columbus was arrested and returned to spain because of his cruel and barbaric attitude towards natives." <<endl;
cout << "John Hanson not George Washington was the first US President." <<endl;
cout << "The first US drug company Bayer invinted Heorin as a pain killer." <<endl;
cout << "At Andrew Jacksons funeral in 1845 his pet parrot had to be removed because it was swearing."<<endl;
cout << "Christmas did not become a national holiday untill 1890."<<endl;
cout << "During the Civil War 22 black soldiers were awarded the Medal Of Honor."<<endl;
cout << "In 1945 Grand Rapids, Michigan became the first city to add flouride to its water supply."<<endl;
cout << "The first American College was Harvard founded in 1636." <<endl;
break;
case 'C':
cout << "American Online was originally designed for users to download games to there Atari 2600 via phone lines." << endl;
cout << "The PSone was first created as a CD add-on for the SNES but because of licensing issues is was developed as its own console." << endl;
cout << "During the 1975 Christmas season PONG was the highest selling game." << endl;
cout << "Super Mario World took 29,000 hours to program." << endl;
cout << "Popular Science named Sega's Dreamcast one of the most innovative products of 1999." << endl;
cout << "The original name for the XBOX console was DirectXbox because of the DirectX developers who crated it" << endl;
cout << "68% of American housholds play console or computer games." << endl;
cout << "40% of all game players are women." << endl;
break;
case 'D':
cout << "Nicholas Cage's real name is Nicholas Coppola." << endl;
cout << "Paul Newman was originally cast to play Cpt Quint in Jaws." << endl;
cout << "During Watergate, Paul Newman was considered a celebrity enemy by Richard Nixon. " << endl;
cout << "Jack Nicholson has not given a talk show interview since 1971." << endl;
cout << "Kevin Bacon has had at least a supporting role in 27% of all American made movies since 1982." << endl;
cout << "Sly Stallone's first role was in an adult film during the 70's for which he was paid 200 dollars." << endl;
cout << "The Dark Knight is the first Batman film not to have Batman in the title." << endl;
cout << "Because of his acting background, Ronald Reagen is the only US president to wear a Nazi Uniform." << endl;
cout << "Sean Connery wore a toupee in every Bond he starred in." << endl;
break;
case 'E':
cout << "The cities of Boston and Detroit have the oldest stadiums in major league baseball." <<endl;
cout << "Innovating the game an MIT instructor added dimples to basbeball bats reducing the drag." <<endl;
cout << "The NBA's coach of the year award is named after coach Red Auerbach." <<endl;
cout << "The most common nickname for major league pitchers is Lefty." <<endl;
cout << "Hank Aaron hit 20 or more homeruns in all 20 seasons played." <<endl;
cout << "Boxing is also known as pugilism and prizefighting." <<endl;
cout << "Rugby was introduced in the US in 1875 but faded as football was introduced." <<endl;
cout << "On a football field there are 19 yard lines that divide the field into 5 yd segments." <<endl;
cout << "*** Footballs weigh between 14 and 15 oz." <<endl;
cout << "In 1936 a game of cricket was disrupted when a player struck a sparrow with the ball. The bird is on display at the Lord Cricket Ground." <<endl;
break;
default:
cout << "ERROR INVALID SELECTION, PLEASE CHOOSE A CATEGORY LISTED." << endl;//error mesage
}
cout << "Please enter your first and last name." << endl;
cin >> firstName >> lastName;
outFile.open("nameFact.txt");
outFile << firstName << lastName << endl;
cout << "Please enter a fact you would like to see added to the program." << endl;
cin.ignore( numeric_limits<streamsize>::max(), '\n');
getline(cin, newFact);
cout << "The fact you entered is: " << newFact << endl;
outFile << newFact << endl;
outFile.close();
return 0;
}
I input what you've shown and got 4 errors that I do not understand. Here are the errors
1) error C2065: 'numeric_limits' : undeclared identifier line 94
2) error C2275: 'std::streamsize' : illegal use of this type as an expression line 94
3) error C2780: 'const _Ty &std::max(const _Ty &,const _Ty &,_Pr)' : expects 3 arguments - 0 provided line 94
4) error C2780: 'const _Ty &std::max(const _Ty &,const _Ty &)' : expects 2 arguments - 0 provided c:\c++ visual line 94
I am not sure what type to declare 'numeric_limits' as maybe string
but the other 3 I have no Idea about.
-
May 13th, 2010, 03:12 PM
#8
-
May 13th, 2010, 03:18 PM
#9
Re: Last Question
Thanks for the input Lindley however that did not fix anything. I will post the full code again
Code:
#include <iostream>
#include <string>
#include <fstream>
#include <limits>
using namespace std;
int main()
{
ofstream outFile;
string firstName, lastName;
string newFact;
string numeric_limits;
char category;//declared variables
cout << "Welcome to the TRIVIA TRAINER! This application will help you prepare" << endl;//game description
cout << "for trivia. Choose one of the categories and a facts about your choosen" <<endl;
cout << "category will appear on the screen." << endl;
cout <<"Please choose a Trivia Trainer category." << endl;
cout <<"A: The Universe" << endl;
cout <<"B: U.S. History" << endl;
cout <<"C: Video Games" << endl;
cout <<"D: Movies" << endl;
cout <<"E: Sports" << endl;
cin >> category;
cout <<"You choose to learn about category: " << category << endl;
switch (category)//switch statment
{
case 'A' :
cout << "In the total universe, it is estimated that there are 10^22 stars." << endl;
cout << "It is estimated that there are 50 thousand million galaxies in the universe." << endl;
cout << "The telescope was invented in 1608 by Hans Lippershey." << endl;
cout << "The earth rotates the sun at 30 kilometers per second." << endl;
cout << "Lightyears are a measure of distance and not time. light travels about 9,500,000,000,000 kilometers in one lightyear."<< endl;
cout << "since there are no air-waves in space sound does not carry therefore you couldn't hear anything." << endl;
cout << "25% of the universe is dark matter,70% is dark energy making only 5% visible to us." << endl;
break;
case 'B':
cout << "Christopher Columbus was arrested and returned to spain because of his cruel and barbaric attitude towards natives." <<endl;
cout << "John Hanson not George Washington was the first US President." <<endl;
cout << "The first US drug company Bayer invinted Heorin as a pain killer." <<endl;
cout << "At Andrew Jacksons funeral in 1845 his pet parrot had to be removed because it was swearing."<<endl;
cout << "Christmas did not become a national holiday untill 1890."<<endl;
cout << "During the Civil War 22 black soldiers were awarded the Medal Of Honor."<<endl;
cout << "In 1945 Grand Rapids, Michigan became the first city to add flouride to its water supply."<<endl;
cout << "The first American College was Harvard founded in 1636." <<endl;
break;
case 'C':
cout << "American Online was originally designed for users to download games to there Atari 2600 via phone lines." << endl;
cout << "The PSone was first created as a CD add-on for the SNES but because of licensing issues is was developed as its own console." << endl;
cout << "During the 1975 Christmas season PONG was the highest selling game." << endl;
cout << "Super Mario World took 29,000 hours to program." << endl;
cout << "Popular Science named Sega's Dreamcast one of the most innovative products of 1999." << endl;
cout << "The original name for the XBOX console was DirectXbox because of the DirectX developers who crated it" << endl;
cout << "68% of American housholds play console or computer games." << endl;
cout << "40% of all game players are women." << endl;
break;
case 'D':
cout << "Nicholas Cage's real name is Nicholas Coppola." << endl;
cout << "Paul Newman was originally cast to play Cpt Quint in Jaws." << endl;
cout << "During Watergate, Paul Newman was considered a celebrity enemy by Richard Nixon. " << endl;
cout << "Jack Nicholson has not given a talk show interview since 1971." << endl;
cout << "Kevin Bacon has had at least a supporting role in 27% of all American made movies since 1982." << endl;
cout << "Sly Stallone's first role was in an adult film during the 70's for which he was paid 200 dollars." << endl;
cout << "The Dark Knight is the first Batman film not to have Batman in the title." << endl;
cout << "Because of his acting background, Ronald Reagen is the only US president to wear a Nazi Uniform." << endl;
cout << "Sean Connery wore a toupee in every Bond he starred in." << endl;
break;
case 'E':
cout << "The cities of Boston and Detroit have the oldest stadiums in major league baseball." <<endl;
cout << "Innovating the game an MIT instructor added dimples to basbeball bats reducing the drag." <<endl;
cout << "The NBA's coach of the year award is named after coach Red Auerbach." <<endl;
cout << "The most common nickname for major league pitchers is Lefty." <<endl;
cout << "Hank Aaron hit 20 or more homeruns in all 20 seasons played." <<endl;
cout << "Boxing is also known as pugilism and prizefighting." <<endl;
cout << "Rugby was introduced in the US in 1875 but faded as football was introduced." <<endl;
cout << "On a football field there are 19 yard lines that divide the field into 5 yd segments." <<endl;
cout << "*** Footballs weigh between 14 and 15 oz." <<endl;
cout << "In 1936 a game of cricket was disrupted when a player struck a sparrow with the ball. The bird is on display at the Lord Cricket Ground." <<endl;
break;
default:
cout << "ERROR INVALID SELECTION, PLEASE CHOOSE A CATEGORY LISTED." << endl;//error mesage
}
cout << "Please enter your first and last name." << endl;
cin >> firstName >> lastName;
outFile.open("nameFact.txt");
outFile << firstName << lastName << endl;
cout << "Please enter a fact you would like to see added to the program." << endl;
cin.ignore( numeric_limits<streamsize>::max(), '\n');
getline(cin, newFact);
cout << "The fact you entered is: " << newFact << endl;
outFile << newFact << endl;
outFile.close();
return 0;
}
still getting the same errors as above minus the C2065.
-
May 13th, 2010, 03:20 PM
#10
Re: Last Question
ok so where I messed up was declaring
numeric_limits
that fixed the problem thanx for the help gents.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|