Here is the sample of my code:
//Function Declarations
void askChampSearch(string&, string&);
void displayChampDetail(string);
int main()
{
string searchedChamp;
string aboutUser;
cout << "**********YOU'RE GOING TO WANT TO MAKE YOUR PROMPT FULL SCREEN**********" << endl;
cout << "Welcome to my LEAGUE OF LEGENDS info guide!" << endl;
cout << "You can search any champion in League of Legends, and all" << endl;
cout << "of that champions information will be displayed.The information" << endl;
cout << "that will be displayed is: Abilites, Champion Lore, About The Campion, Best Build, and the Champions Wiki Page!" << endl;
cout << endl;
askChampSearch(aboutUser, searchedChamp);
displayChampDetail(searchedChamp);
return 0;
}
//Function Definitions
void askChampSearch(string& UserName, string& searchedChamp){
cout << "Thank you for trying out my program! I really appreciate it! What is your name? (Don't worry, your name isn't stored) ";
cin >> UserName;
cout << "Hi " << UserName << ", nice to meet you!" << endl;
cout << endl;
cout << "What League of Legends champion would you like to search? ";
cin >> searchedChamp;
cout << "The champion that you have searched for was " << searchedChamp << "!";
}
void displayChampDetail(string searchedChamp) {
if (searchedChamp == Ahri) {
cout << "Ahri is a nine-tailed fox who is a whore" << endl;
cout << "If Ahri hits you with a charm, you are fucked" << endl;
}
}
What I am having problems with is after my program leaves the first void:
void askChampSearch(string& UserName, string& searchedChamp){
cout << "Thank you for trying out my program! I really appreciate it! What is your name? (Don't worry, your name isn't stored) ";
cin >> UserName;
cout << "Hi " << UserName << ", nice to meet you!" << endl;
cout << endl;
cout << "What League of Legends champion would you like to search? ";
cin >> searchedChamp;
cout << "The champion that you have searched for was " << searchedChamp << "!";
}
It won't enter into the next one! For example. If the user types the Name Ahri it should display what I have done, but it says I need to define Ahri. How do I define Ahri within the second Void. Here is the code for my second Void:
void displayChampDetail(string searchedChamp) {
if (searchedChamp == Ahri) {
cout << "Ahri is a nine-tailed fox" << endl;
cout << "If Ahri hits you with a charm, you are dead" << endl;
}
}
What I am trying to do is, if the user types the name Ahri, it the information that I have provided.
The error code that I am getting is identifier "Ahri" is undefined.
searchedChamp == "Ahri"
It is a string. Other wise compiler tries to find a variable with name "Ahri"and it doesn't find it so it asks you to define it.
Related
Then it will reset to the questions number 1.
I wanted to implement health points system in my code, so that if your hp goes to 0 (zero) when choosing the wrong answer, it will start to question number 1
I'm new to c++ and doesn't know much about it but if you have any recommendation how to improve my coding i'm happy to take your advice.
Code:
void questions()
{
int score, end, hp = 1;
char ans[28];
cout <<"\t\tHEALTH POINTS= " << hp <<"\n\n";
cout << "1.What thing has to be broken before it can be used?\n\n"; //Questions
cout << "[A]-Egg,";
cout << " [B]-Heart,"; //Choices
cout << " [C]-Cube,";
cout << " [D]-Case";
cout << "\n\n";
cout << "YOUR ANSWER IS: ";
cin >> ans[1];
if (ans[1]=='a'||ans[1]=='A') //This will decide if the input is correct
{
cout << "YOUR ANSWER IS CORRECT: [A] - Egg \n\n";
score++;
}
else
{
cout <<"\nWRONG! ";
cout <<"YOU NOW HAVE "<< (hp=(hp-1)) <<" HP LEFT\n\n";
}
cout << "2.Jimmy's mother had three children. The first was called April, \nthe second was called May. What was the name of the third?\n";
cout << "[A]-May,";
cout << " [B]-Jimmy,";
cout << " [C]-April,";
cout << " [D]-Third";
cout << "\n\n";
cout << "Your Answer is: ";
cin >> ans[2];
if (ans[2]=='b'||ans[2]=='B')
{
cout << "YOUR ANSWER IS CORRECT: [B] - Jimmy \n\n";
score++;
}
else
{
cout <<"\nWRONG! ";
cout <<"YOU NOW HAVE "<< (hp=(hp-1)) <<" HP LEFT\n\n";
}
cout << "\n\t\t YOUR SCORE IS:" << score << "/2, ";
cout <<"YOU HAVE "<< hp <<" HP LEFT\n\n";
cout << endl;
cout <<"\n\t\t PRESS ANY KEY TO GO BACK TO CHOICES...";
getch(); //Holds the screen
system("cls");
questions();
One way to improve your approach might be implementing some sort of function to handle asking a question, with predefined choices, and getting an answer back. Instead of writing the code out twice like you do above to ask two questions, you could call the same function twice, passing in the different arguments.
my C++ class just started learning about developing structures. I'm stuck on a homework problem where I'm asked to write a program that uses a structure named movie_data and two movie_data variables to display information about a movie. I'm able to develop the movie_data structure correctly along with the two variable to outsoruce to a function named get_movie_info. However, because I set it as a void function, I'm unable to return anything produced by the get_movie_function to send to my movie_display function. I tried rewriting my functions to be of the movie_data structure data type, but that seemed to make things worse. The first function produces all the information correctly, but the second function doesn't output anything. Thank you for your time.
#include <iostream>
#include <iomanip>
using namespace std;
struct movie_data
{
string title;
string director;
int year_released;
int running_time;
};
//Function Prototype
void get_movie_info(movie_data movie1, movie_data movie2);
void movie_display(movie_data movie1, movie_data movie2);
int main()
{
movie_data movie1;
movie_data movie2;
get_movie_info(movie1, movie2);
movie_display(movie1, movie2);
return 0;
}
void get_movie_info(movie_data movie1, movie_data movie2)
{
//Get movie_data's title
cout << "Enter the title for the first movie: ";
//cin.ignore();
getline(cin, movie1.title);
cout << movie1.title << endl;
//Get movie_data's director
cout << "Enter the director's name for " << movie1.title << ": ";
//cin.ignore();
getline(cin, movie1.director);
cout << movie1.director << endl;
//Get movie_data's release year
cout << "Enter the release year for " << movie1.title << ": ";
cin >> movie1.year_released;
cout << movie1.year_released << endl;
//Get movie_data's running time
cout << "Enter the runtime of " << movie1.title << " in minutes: ";
cin >> movie1.running_time;
cout << movie1.running_time << " minutes" << endl;
//Get movie_data's title
cout << "Enter the title for the second movie: ";
cin.ignore();
getline(cin, movie2.title);
cout << movie2.title << endl;
//Get movie_data's director
cout << "Enter the director's name for " << movie2.title << ": ";
//cin.ignore();
getline(cin, movie2.director);
cout << movie2.director << endl;
//Get movie_data's release year
cout << "Enter the release year for " << movie2.title << ": ";
cin >> movie2.year_released;
cout << movie2.year_released << endl;
//Get movie_data's running time
cout << "Enter the runtime of " << movie2.title << " in minutes: ";
cin >> movie2.running_time;
cout << movie2.running_time << " minutes" << endl;
}
void movie_display(movie_data movie1, movie_data movie2)
{
//Display movie1 information
cout << "\nBelow is the data of the first movie:\n";
cout << "Movie Title: " << movie1.title << endl;
cout << "Director's Name: " << movie1.director << endl;
cout << "Release Year: " << movie1.year_released << endl;
cout << "Movie Runtime in minutes: " << movie1.running_time << endl;
//Display the movie information
cout << "\nBelow is the data of the second movie:\n";
cout << "Movie Title: " << movie2.title << endl;
cout << "Director's Name: " << movie2.director << endl;
cout << "Release Year: " << movie2.year_released << endl;
cout << "Movie Runtime in minutes: " << movie2.running_time << endl;
}
While #Kai's answer of using refrences would work and correctly would answer your original question, I suggest doing something else.
First, use a function to read in only one move_data and make it return that:
movie_data get_movie_info();
A possible implementation (using your code) could be like this:
movie_data get_movie_info(){
movie_data movie;
cout << "Enter the title for the first movie: ";
getline(cin, movie.title);
cout << "Enter the director's name for " << movie.title << ": ";
getline(cin, movie.director);
cout << "Enter the release year for " << movie.title << ": ";
cin >> movie.year_released;
cout << "Enter the runtime of " << movie.title << " in minutes: ";
cin >> movie.running_time;
return movie;
}
Now you can call it twice to read your info and it will return the movie data as the correct structure.
movie_data movie1 = get_movie_data();
If you need to have structs that can be edited, references are a good choice. For returning multiple values, there are better choices: An array of a suitable size (std::array), a Pair for two, or a vector of Objects.
It's better to avoid having output parameters (as a rule of thumb, break it if you need to and know why) as they are hard to grasp from the signature and hard to keep track of.
Notice, that you do everything twice. The point of using functions, is to not do everything twice, so you should write one function to do one thing and just call it with different parameters. For example in get_movie_info. A better design would be to create a function that creates exactly one movie_data and returns it:
movie_data get_movie_info()
{
movie_data result = {}; // That's the variable were we store the data
//Get movie_data's title ...
//Get movie_data's director ...
//Get movie_data's release year ...
//Get movie_data's running time ...
return result; // return the created movie data
}
The same goes for movie_display. Don't create a function that does exactly the same thing for two parameters, but create a function that does it one time and call it twice:
void movie_display(movie_data movie)
{
cout << "Movie Title: " << movie.title << endl;
//And so on ...
}
Then you combine both in the main like this:
int main()
{
movie_data movie1 = get_movie_info();
movie_data movie2 = get_movie_info();
std::cout << "data of the first movie:\n";
movie_display(movie1);
std::cout << "data of the second movie:\n";
movie_display(movie2);
return 0;
}
Update your function signatures to take references instead of values.
https://www.learncpp.com/cpp-tutorial/72-passing-arguments-by-value/
void get_movie_info(movie_data& movie1, movie_data& movie2)
void movie_display(const movie_data& movie1, const movie_data& movie2)
i'm trying to set up a simple flight booking system program, but my second bit of cin code is not calling for input when i run the program. Unlike the initial cin that requires your name initially. the program just runs and returns 0. I'm a beginner at c++ and i know this is a simple fix so please be understanding . Thank you any guidance will be greatly appreciated.
#include <iostream>
#include <sstream>
using namespace std;
int main()
{
int name;
int Seatnumber;
int optionnumber = 1-5 ;
std::string out_string;
std::stringstream ss;
ss << optionnumber;
out_string = ss.str();
cout << "Welcome to the COS1511 Flight Booking System" "\n" << endl;
cout << "Enter full name : " << endl;
cin >> name ; "\n";
cout << "\n" "The Available travel times for flights are:" << endl;
cout << " Depart Arrive" << endl;
cout << "1. 7.00 9.30" << endl;
cout << "2. 9.00 11.30" << endl;
cout << "3. 11.00 13.30" << endl;
cout << "4. 13.00 15.30" << endl;
cout << "5. 15.00 17.30" << endl;
cout << "Choose the time by entering the option number from the displayed list : " << endl;
cin >> optionnumber ;
if (optionnumber == 1-5){
cout << "\n" "The available seats for are as follows " << endl;
}
else
cout << "Incorrect option! Please Choose from 1-5 " << endl;
cout << "First Class(1920)" << endl;
cout << "|A1||A2||A3|----|A4||A5||A6|" << endl;
cout << "|B1||B2||B3|----|B4||B5||B6|" << endl;
cout << "|C1||C2||C3|----|C4||C5||C6|" << endl;
cout << "|D1||D2||D3|----|D4||D5||D6|" << endl;
cout << "| Economy Class(1600)" << endl;
cout << "|E1||E2||E3|----|E4||E5||E6|" << endl;
cout << "|F1||F2||F3|----|F4||F5||F6|" << endl;
cout << "|G1||G2||G3|----|G4||G5||G6|" << endl;
cout << "|H1||H2||H3|----|H4||H5||H6|" << endl;
cout << "|I1||I2|" << endl;
cout << "Please Key in a seat number to choose a seat(eg: A2)" << endl;
cin >> Seatnumber;
}
prompt the user to enter their name.
Then display a menu showing the available times for the flight.
the user can choose a preferred departure time(option 1-5)
the option selected should be validated for 1-5
if the user entered the correct time a seating arrangement for that particular flight time should be displayed to the next user for the user to choose a seat.
Warning
int optionnumber = 1-5 ;
does
int optionnumber = -4 ;
and
if (optionnumber == 1-5){
does
if (optionnumber == -4){
but you wanted if ((optionnumber >= 1) && (optionnumber <= 5))
if the user entered the correct time a seating arrangement for that particular flight time should be displayed to the next user for the user to choose a seat.
No, whatever the result of the test above you continue and write "First Class(1920)" etc so even when the choice is invalid
in
cin >> name ; "\n";
what did you expect about the "\n" ?
I encourage you to check the read success, currently if the user does not enter an integer you do not know that
But are you sure the name must be an integer ? Probably it must be s string
out_string is unused, it can be removed
Visibly Seatnumber is not an int but a string (A1 ...)
you probably want to loop until a valid time is enter, also fixing the other problems a solution can be :
#include <iostream>
#include <string>
using namespace std;
int main()
{
string name;
string Seatnumber;
int optionnumber;
cout << "Welcome to the COS1511 Flight Booking System" "\n" << endl;
cout << "Enter full name : " << endl;
if (!(cin >> name))
// EOF (input from a file)
return -1;
cout << "\n" "The Available travel times for flights are:" << endl;
cout << " Depart Arrive" << endl;
cout << "1. 7.00 9.30" << endl;
cout << "2. 9.00 11.30" << endl;
cout << "3. 11.00 13.30" << endl;
cout << "4. 13.00 15.30" << endl;
cout << "5. 15.00 17.30" << endl;
cout << "Choose the time by entering the option number from the displayed list : " << endl;
for (;;) {
if (!(cin >> optionnumber)) {
// not an int
cin.clear(); // clear error
string s;
// flush invalid input
if (!(cin >> s))
// EOF (input from a file)
return -1;
}
else if ((optionnumber >= 1) && (optionnumber <= 5))
// valid choice
break;
cout << "Incorrect option! Please Choose from 1-5 " << endl;
}
cout << "\n" "The available seats for are as follows " << endl;
cout << "First Class(1920)" << endl;
cout << "|A1||A2||A3|----|A4||A5||A6|" << endl;
cout << "|B1||B2||B3|----|B4||B5||B6|" << endl;
cout << "|C1||C2||C3|----|C4||C5||C6|" << endl;
cout << "|D1||D2||D3|----|D4||D5||D6|" << endl;
cout << "| Economy Class(1600)" << endl;
cout << "|E1||E2||E3|----|E4||E5||E6|" << endl;
cout << "|F1||F2||F3|----|F4||F5||F6|" << endl;
cout << "|G1||G2||G3|----|G4||G5||G6|" << endl;
cout << "|H1||H2||H3|----|H4||H5||H6|" << endl;
cout << "|I1||I2|" << endl;
cout << "Please Key in a seat number to choose a seat(eg: A2)" << endl;
cin >> Seatnumber;
return 0;
}
Compilation and execution :
pi#raspberrypi:/tmp $ g++ -pedantic -Wextra -Wall cc.cc
pi#raspberrypi:/tmp $ ./a.out
Welcome to the COS1511 Flight Booking System
Enter full name :
bruno
The Available travel times for flights are:
Depart Arrive
1. 7.00 9.30
2. 9.00 11.30
3. 11.00 13.30
4. 13.00 15.30
5. 15.00 17.30
Choose the time by entering the option number from the displayed list :
aze
Incorrect option! Please Choose from 1-5
7
Incorrect option! Please Choose from 1-5
2
The available seats for are as follows
First Class(1920)
|A1||A2||A3|----|A4||A5||A6|
|B1||B2||B3|----|B4||B5||B6|
|C1||C2||C3|----|C4||C5||C6|
|D1||D2||D3|----|D4||D5||D6|
| Economy Class(1600)
|E1||E2||E3|----|E4||E5||E6|
|F1||F2||F3|----|F4||F5||F6|
|G1||G2||G3|----|G4||G5||G6|
|H1||H2||H3|----|H4||H5||H6|
|I1||I2|
Please Key in a seat number to choose a seat(eg: A2)
qsd
pi#raspberrypi:/tmp $
Note entering the name with cin >> name does not allow it to contain several names separated by a space, to allow composed name getline can be used
Okay so as the title said its refusing to execute the stuff right under the "do" function even though as far as i can tell all the parameters for a repeat have been fulfilled. So far what i get when i run the program is something along the lines of...
"Would you like to search another name?
Please enter Y for yes and n for no:"
looping over and over when i press y
#include <iostream>
#include <string>
#include <iomanip>
#include <vector>
#include <algorithm>
#include <cstdlib>
using namespace std;
int main()
{
vector <string> vName, vID, vClass;
string sName, sID, sClass, sSearch, cQuestion;
int iSize, iStudent;
// Display initial vector size
iSize = vName.size();
cout << "Student list starts with the size:" << iSize << endl;
// Get size of list from user
cout << "How many students would you like to add?" << endl;
cin >> iStudent;
cin.ignore();
// Get names, ids, and classes
for (int i = 0; i < iStudent; i++)
{
cout << "Student" << i + 1 << ":\n";
cout << "Please enter the student name: ";
getline(cin, sName);
vName.push_back(sName);
cout << "Enter ID number ";
getline(cin, sID);
vID.push_back(sID);
cout << "Enter class name ";
getline(cin, sClass);
vClass.push_back(sClass);
}
// Display header
cout << "The list of students has the size of: " << iStudent << endl;
cout << "The Student List" << endl;
cout << "\n";
cout << "Name:" << setw(30) << "ID:" << setw(38) << "Enrolled Class : " << endl;
cout << "--------------------------------------------------------------------------";
cout << "\n";
// for loop for displying list
for (int x = 0; x < vName.size() && vID.size() && vClass.size(); x++)
{
cout << vName[x] << "\t \t \t" << vID[x] << "\t \t \t" << vClass[x] << endl;
}
// Sorting function
cout << "\n";
cout << "The Student List after Sorting:" << endl;
cout << "\n";
sort(vName.begin(), vName.end());
for (int y = 0; y < vName.size(); y++)
{
cout << vName[y] << endl;
}
cout << "\n";
// Search function
do
{
cout << "Please Enter a name to be searched:" << endl;
getline(cin, sSearch);
if (binary_search(vName.begin(), vName.end(), sSearch))
{
cout << sSearch << " was found." << endl << endl;
}
else
{
cout << sSearch << " was not found." << endl << endl;
}
cout << "Would you like to search another name?" << endl << endl;
cout << "Please enter Y for Yes and N for No:" << endl << endl;
cin >> cQuestion;
} while (cQuestion == "Y" || cQuestion == "y");
cout << "Thank you for using this program!" << endl;
return 0;
}
Edit:
Posted whole program, please excuse any grammatical mistakes, I'm just trying to get the program down before i go in there and make it pretty.
The tail of your loop does this:
cout << "Please enter Y for Yes and N for No:" << endl << endl;
cin >> cQuestion;
which will consume your string if you entered one, but leave the trailing newline in the input stream. Thus when you return to the top of the loop after entering Y or y, and do this:
cout << "Please Enter a name to be searched:" << endl;
getline(cin, sSearch);
the getline will extract an empty line.
How to consume the unread newline from the input stream is up to you. You will likely just end up using .ignore() as you did prior in your program. Or use getline to consume cQuestion. You have options. Pick one that works.
And as a side note, I would strongly advise you check your stream operations for success before assuming they "just worked". That is a hard, but necessary, habit to break. Something like this:
do
{
cout << "Please Enter a name to be searched:" << endl;
if (!getline(cin, sSearch))
break;
if (binary_search(vName.begin(), vName.end(), sSearch))
{
cout << sSearch << " was found." << endl << endl;
}
else
{
cout << sSearch << " was not found." << endl << endl;
}
cout << "Would you like to search another name?" << endl << endl;
cout << "Please enter Y for Yes and N for No:" << endl << endl;
} while (getline(cin,cQuestion) && (cQuestion == "Y" || cQuestion == "y"));
If cQuestion is a char array then you need to use strcmp or stricmp to compare it with another string i.e. "Y" and "y" in this case. If cQuestion is a single char then you need to compare with 'Y' and 'y' (i.e. with a single quote)
Strings in C++ are not first class types therefore they do not have some of the string operation that exist for other basic types like ints and floats. You do have std::string as part of the standard C++ library which almost fulfills the void.
If you just change the type of cQuestion to std::string your code should work but if you want to stick with chars then you will need to change the quote style.
I have the following code. Everything works fine but quite different. I expect the first three cout << come one after the another, so when the first message is shown in the console, the user enters the value, and then the next cout << shows another message and the user enters the name of the book, and then the third cout << shows the last message and the user enters the year. But it shows the first message for, I enter the value, and then it shows the next two messages together. Why?
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
string AuthorName;
string AuthorBook;
string YearPublished;
cout << "Please Enter the Author Name" << endl;
cin >> AuthorName;
cout << "Please enter the Author Book" << endl;
cin >> AuthorBook;
cout << "Please enter the year when the book was published" << endl;
cin >> YearPublished;
cout << setw(15) << "Author Name";
cout << setw(15) << "Prominent Work";
cout << setw(15) << "Year Published";
cout << endl << endl;
cout << setw(15) << AuthorName;
cout << setw(15) << AuthorBook;
cout << setw(15) << YearPublished;
cout << endl << endl;
return 0;
}
You need to use getline() since C++ stops reading your input at the first space in strings using cin.