Skip to content

Instantly share code, notes, and snippets.

@mshafae
Created February 16, 2023 21:51
Show Gist options
  • Save mshafae/3c1ccc78f58ac83491d51deef4d371bb to your computer and use it in GitHub Desktop.
Save mshafae/3c1ccc78f58ac83491d51deef4d371bb to your computer and use it in GitHub Desktop.
CPSC 120 Example showing different ways to prompt the computer user for input.
// Gist https://gist.github.com/70457715ff7fc2fcdcdaf44437624dd0
#include <iostream>
#include <string>
std::string PromptForString(std::string query) {
std::cout << query;
std::string answer;
std::cin >> answer;
return answer;
}
int PromptForInt(std::string query) {
std::cout << query;
int answer;
std::cin >> answer;
return answer;
}
double PromptForDouble(std::string query) {
std::cout << query;
double answer;
std::cin >> answer;
return answer;
}
int main(int argc, char const *argv[]) {
std::cout << "What's your favorite color? ";
std::string favorite_color;
std::cin >> favorite_color;
std::cout << "Your favorite color is " << favorite_color
<< "! That's mine too!\n";
std::string favorite_animal{PromptForString("What's your favorite animal? ")};
std::cout << "Your favorite animal is " << favorite_animal
<< "! That's mine too!\n";
int favorite_number{PromptForInt("What's your favorite number? ")};
std::cout << "Your favorite number is " << favorite_number
<< "! That's mine too!\n";
double favorite_decimal{
PromptForDouble("What's your favorite decimal number? ")};
std::cout << "Your favorite decimal number is " << favorite_decimal
<< "! That's mine too!\n";
int number_of_cats{std::stoi(PromptForString("How many cats do you have? "))};
std::cout << "You have " << number_of_cats << "! That's a lot!\n";
double stick_of_gum_price{std::stod(PromptForString(
"How many dollars and cents would you pay for a stick of gum? "))};
std::cout << "If you paid $" << stick_of_gum_price
<< ", then you paid too much.\n";
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment