BIG EDIT: I added in and it now detects string inputs :)!
if (false == isdigit(grade[0])
edit: The problem: can not determine if I put in a string instead of a float or int.
I've been looking all through this website and there's many posting about checking string input. Here is my take on trying to combine everything I learned, and as you can see, It's not doing what I want it to do.
Such inputs and what happens:
user input: "1.2" : can tell it's a float
user input: "1" : can tell it's a int
user input: "a": ERROR : think's it's a float. This is the problem. If I enter in 'a' I need the program to recognize that a string was inputted and this calling in certain functions.
Some guidance would be much appreciated:
#include "assessGrade.h"
int main(void)
{
int finalGrade = 0;
int gradeHolder = 0;
int numberGrade[5] = { 0,0,0,0,0 };
double doubleGrade = 0;
//my beloved bool's
bool floatGrade = false;
bool intGrade = false;
bool stringGrade = false;
//======================================//
// Getting the input from user //
// Also check for stringfloat/int type //
//======================================//
std::string grade;
std::getline(std::cin, grade);
std::stringstream stream(grade);
if (false == isdigit(grade[0]))
{
//it's not a digit
cout << "STRING: " << grade << endl;
stringGrade = true;
}
else if(!(stream >> gradeHolder) || grade.find('.') != std::string::npos)
{
//its a float!
cout << "FLOAT: " << grade << endl;
floatGrade = true;
}
else
{
//it's a int!
cout << "INTEGER: " << grade << endl;
intGrade = true;
}
//======================================//
// Parsing the input //
//======================================//
if (intGrade == true)
{
//do int stuff here
}
else if (floatGrade == true)
{
//do float stuff here
//doubleGrade = grade;
}
else
{
//do string stuff here
}
return 0;
}
It's far from easy to determine exactly what a "float value" would be. Is +1.0E-02 a float variable? It's got a letter in it!
You almost got the decent solution. Use std::stringstream to see if you can extract an integer or float value. You will need to .clear() it, if the first attempt fails.
Related
I did my "Hello World", I'm just getting started on my programming adventure with C++. Here is the first thing I've written, what are some ways to get it to end with user input? I'd like a yes or no option that would terminate the program. Also any feedback is welcome, thank you
#include <iostream>
using namespace std;
void Welcome();
void calculateNum();
void tryAgain();
int main() {
Welcome();
while (true) {
calculateNum();
tryAgain();
}
system("pause");
}
void calculateNum() {
float userNumber;
cin >> userNumber;
for (int i = 100; i >= 1; i--) {
float cNumber = i* userNumber;
cout << i << " >>>>> " << cNumber << endl;
}
}
void Welcome() {
cout << "Welcome \n Enter a number to see the first 100 multiples \n";
}
void tryAgain() {
cout << "Try again? Enter another number... ";
}
Here is one option:
Switch to do ... while loop, with the condition at the end.
Make your tryAgain() function return a boolean and put it in the while condition.
In tryAgain function read input from the user, and compare it to expected answers.
First, lets add a new header for string, it will make some things easier:
#include <string>
Second, lets rebuild the loop:
do {
calculateNum();
} while (tryAgain());
And finally, lets modify the function:
bool tryAgain() {
string answer;
cout << "Try again? (yes / no)\n";
cin >> answer;
if (answer == "yes") return true;
return false;
}
Now, there is a slightly shorter way to write that return, but it might be confusing for new learners:
return answer == "yes";
You don't need the if because == is an operator that returns bool type value.
You can change your calculateNum() in the following way:
Change the return value of your calculateNum() function into bool to indicate whether the program shall continue or stop
read the input into a std::string
check if the string is equal to your exit string like 'q' for quit
3.a in that case, your function returns false to indicate the caller that the program shall stop
3.b otherwise, create a stringstream with your string and read the content of the stream into your float variable and continue as you do like now
In your loop in your main function you break if calculateNum() returned false
Here is a simple solution:
#include <iostream>
// Here are two new Includes!
#include <sstream>
#include <string>
using namespace std;
void Welcome();
// Change return value of calculateNum()
bool calculateNum();
void tryAgain();
int main()
{
Welcome();
while (true)
{
if (!calculateNum())
break;
tryAgain();
}
system("pause");
}
bool calculateNum()
{
//Read input into string
string userInput;
cin >> userInput;
//Check for quit - string - here just simple q
if (userInput == "q")
return false;
//otherwise use a std::stringstream to read the string into a float as done before from cin.
float userNumber;
stringstream ss(userInput);
ss >> userNumber;
//and proces your numbers as before
for (int i = 100; i >= 1; i--)
{
float cNumber = i * userNumber;
cout << i << " >>>>> " << cNumber << endl;
}
return true;
}
void Welcome()
{
cout << "Welcome \n Enter a number to see the first 100 multiples \n";
}
void tryAgain()
{
cout << "Try again? Enter another number... ";
}
Having your users input in a string you can even do further checks like checking if the user entered a valid number, interpret localized numbers like . and , for decimal delimitters depending on your system settings and so on.
Trying to create a program that takes a coffee flavor add-in and checks if it's valid using an array.
If valid it uses the array index to gather price information.
I managed to write the code below, but it only works for 1 iteration.
How can alter it so a user can enter: Cream and cinnamon and output the total of each add-in as well as the total price of the cup of coffee? The cup of coffee starts with a base price of $2.00
#include <iostream>
#include <string>
using namespace std;
int main()
{
// Declare variables.
string addIn; // Add-in ordered
const int NUM_ITEMS = 5; // Named constant
// Initialized array of add-ins
string addIns[] = { "Cream", "Cinnamon", "Chocolate", "Amaretto", "Whiskey" };
// Initialized array of add-in prices
double addInPrices[] = { .89, .25, .59, 1.50, 1.75 };
bool foundIt = false; // Flag variable
int x; // Loop control variable
double orderTotal = 2.00; // All orders start with a 2.00 charge
string QUIT = "XXX";
// Get user input
cout << "Enter coffee add-in or XXX to quit: ";
cin >> addIn;
// Write the rest of the program here.
for (x = 0; x < NUM_ITEMS && foundIt == false && addIn != QUIT; x++) {
if (addIn == addIns[x]) {
foundIt = true;
x--;
}
}
if (foundIt == true) {
cout << addIns[x] << " $" << addInPrices[x] <<endl;
cout << "$" << orderTotal + addInPrices[x] <<endl;
}
else {
cout << "Sorry, we do not carry that." <<endl;
cout << "Order total is $ " << orderTotal <<endl;
}
return 0;
}
Don't use parallel arrays - you will mess up maintaining them.
Better options:
Create a struct for your add-ins:
struct Addin {
std::string name;
double price;
}
and use an array (or better yet an std::vector) of those structs.
Another option is to use a map:
std::map<std::string, double> addIns = {
{"Cream", .89},
{"Cinnamon", .25},
// etc.
};
Then you won't need a loop, just a lookup
auto item = addIns.find(addIn);
if(item != addIns.end() {
// do your math here
}
Your program is written to get a single output. For multiple outputs there have to be loops and the not found condition also has to be re-written.
try this
#include <iostream>
#include <string>
using namespace std;
int main()
{
// Declare variables.
const int NUM_ITEMS = 5; // Named constant
string addIn[NUM_ITEMS]; // Add-in ordered
// Initialized array of add-ins
string addIns[] = { "Cream", "Cinnamon", "Chocolate", "Amaretto", "Whiskey" };
// Initialized array of add-in prices
double addInPrices[] = { .89, .25, .59, 1.50, 1.75 };
//bool foundIt = false; // Flag variable
int x, i, j; // Loop control variable
double orderTotal = 2.00; // All orders start with a 2.00 charge
string QUIT = "XXX";
// Get user input
cout << "Enter coffee add-ins followed by XXX to quit: ";
for(i=0; i<NUM_ITEMS; i++) {
cin >> addIn[i];
if(addIn[i] == QUIT) {
i++;
break;
}
}
int foundIt[i];
// Write the rest of the program here.
for(j=0; j<i; j++) {
foundIt[j] = -1;
for(x = 0; x<NUM_ITEMS && foundIt[j] == -1 && addIn[j] != QUIT; x++) {
if (addIn[j] == addIns[x]) {
foundIt[j] = x;
}
}
}
for(j=0; j<i-1; j++) {
cout << addIn[j];
if(foundIt[j] != -1) {
cout << " $" << addInPrices[foundIt[j]] << endl;
orderTotal = orderTotal + addInPrices[foundIt[j]];
}
else {
cout << " - Sorry, we do not carry that." <<endl;
}
}
cout << "$" << orderTotal <<endl;
return 0;
}
Sample Outputs
Enter coffee add-ins followed by XXX to quit: Cream Cinnamon XXX
Cream $0.89
Cinnamon $0.25
$3.14
Enter coffee add-ins followed by XXX to quit: Cream Onion XXX
Cream $0.89
Onion - Sorry, we do not carry that.
$2.89
What I did was made addIn array of srings with NUM_ITEMS size instead of variable. Also, foundIt was made an integer array to keep track of indexes where the items are found in addIns array and -1 if not found.
To only access the items that user has entered in addIn, your QUIT has been made the termination condition in that array.
The structure you are looking for is a while or do/while loop.
To be able to enter "empty" lines use std::getline from.
The structure of your program will then look something like this :
As you can see I have a habit of changing my boolean expressions into functions (predicates). This makes code more readable and predicates reusable in other bits of code.
#include <iostream>
#include <string>
bool is_quit(const std::string& input)
{
return input.length() > 0;
}
bool is_valid_input(const std::string& input)
{
return true; // your own check
}
int main()
{
bool quit = false;
std::string input;
do
{
std::cout << "give input : ";
std::getline(std::cin, input);
quit = is_quit(input);
if (is_valid_input(input) && !quit)
{
std::cout << "ok" << std::endl;
}
} while (!quit);
return 0;
}
The program won't compile in Visual Studio Community, I have no idea why. I've looked into the function and can't seem to find an answer. Any help would be appreciated as this is for my module at university. I'm a little new to C++ but have some experience in other languages. I just can't seem to find how to get the program to run. I get the below error codes when compiling:
main.cpp: In function ‘void runGame(std::__cxx11::string*, std::__cxx11::string*, int)’:
main.cpp:115:53: error: cannot convert ‘std::__cxx11::string {aka std::__cxx11::basic_string<char>}’
to ‘std::__cxx11::string* {aka std::__cxx11::basic_string<char>*}’
for argument ‘1’ to ‘std::__cxx11::string answersGiven(std::__cxx11::string*, std::__cxx11::string*, int, int)’
answersGiven(correctAns[0], wrongAns[0], max, max2);
Here is the Code:
#include<iostream>
#include<string>
using namespace std;
int inputCheck(string menu, int limit); // Validates User Input for topic and difficulty selection.
void runGame(string questions[], string answers[], int level); // Function runs game based on questions.
string modifyQuestion(string q, int mod); // Function modifies question based on difficulty level.
string answersGiven(string correct[], string wrong[], int maxC, int maxW);
int main() {
string questions[3][4] = { // 2D array storing answers. Each row stores a Country.
{ "England", "Wales", "France", "Germany" }, // Stores Europe Questions.
{ "Brazil", "Peru", "Argentina", "Columbia" }, // Stores Additional Questions.
{ "Q1", "Q2", "Q3", "Q4" } // Stores South American Questions.
};
string answers[3][4] = { // 2D array storing answers. Each row stores a city.
{"London", "Cardiff", "Paris", "Berlin"}, // Europe Answers.
{"Brasila", "Lima", "Buenos Aires", "Bogota"}, // South America Answers.
{ "A1", "A2", "A3", "A4" } // Stores additional answers.
};
string topics[3] = { "Capital Cities", "" };
char replay; // Variable to check if the user wants to continue playing.
do {
cout << "Please choose one topic for the quiz:\n";
int choice = inputCheck("1: Europe\n2: South America\n3: Testing", 3); // Gets topic choice via validation function.
cout << "\nNow Select a Difficulty:\n";
int difficulty = inputCheck("1: Easy\n2: Medium\n3: Hard", 3); // Gets difficulty choice via validation function.
choice--;
// run the game
runGame(questions[choice], answers[choice], difficulty);
// Checks if user wants to replay game.
cout << "Do you want to play again? Press (Y) to replay: ";
cin >> replay; // Grabs input from user.
replay = toupper(replay); // Sets data to upper case.
} while (replay == 'Y');
return 0;
}
int inputCheck(string menu, int limit) {
cout << menu << endl; // Presents menu of options.
int num; // Variable to store user's input.
cin >> num; // gets user's input.
while (cin.fail() || num < 1 || num > limit) {
cout << "Invalid Input..." << endl; // Outputs error message.
cout << menu << endl; // Presents menu again.
cin.clear(); // Clearsinput error glag.
cin.ignore(1000, '\n'); // Ignores previous inputted data.
cin >> num; // grabs input again.
}
return num;
}
void runGame(string questions[], string answers[], int level) {
string correctAns[] = {};
string wrongAns[] = {};
cin.ignore(1000, '\n'); // Ignore any trailing enter keys in input stream.
int lives = 3; // Stores the amount of users lives.
string answer; // Stores user's answer.
int count = 0; // Keeps track of the current question.
int score = 0; // Keeps track of user's score.
while (lives > 0 && count < 4) {
string question = questions[count]; // Gets current question from array.
// Modifying question if medium or hard difficulty selected.
if (level == 2) {
// Medium
question = modifyQuestion(question, 6);
}
else if (level == 3) {
// Hard
question = modifyQuestion(question, 2);
}
// Asking Question.
cout << "What is the capital city of " << question << endl; // Asks user the question.
getline(cin, answer); // Gets answer from user, includes answers that are more than one word.
answer[0] = toupper(answer[0]); // Converts first character to upper case.
int max = 0;
int max2 = 0;
if (answer == answers[count]) { // Checks user's answer against current answer.
cout << "Well Done! You've got one right" << endl; // Correct answer message.
count++; // Adds one to question counter.
score++; // Adds one to score.
correctAns[count] = answer;
max++;
}
else {
lives--; // Deducts one from lives.
cout << "Oops, Not quite right... You have " << lives << " lives remaining."; // Outputs incorrect message.
wrongAns[count] = answer;
max2++;
}
answersGiven(correctAns[0], wrongAns[0], max, max2);
}
if (lives == 0 && score == 0) { // Checks if user didnt get anything correct.
cout << "Wow.. 0 / 4. Thats not good." << endl;
}
else {
cout << "You got " << score << " Correct and had " << lives << " lives remaining" << endl;
}
cout << "\nThese are the questions you got wrong:\n";
}
string modifyQuestion(string q, int mod) {
for (int i = 0; i < q.length(); i++) {
if (i % mod == 0) { // If counter is divisible by the mod value.
q[i] = '*'; // Change current letter to an aterix.
}
}
return q; // return modified string back to the function.
}
string answersGiven(string correct[], string wrong[], int maxC, int maxW) {
for (int i = 0; i < maxC; i++) {
cout << correct[i] << " - Correct" << endl;
}
for (int i = 0; i < maxW; i++) {
cout << wrong[i] << " - Incorrect" << endl;
}
}
I input a number in char type variable. like 12 or 22. but, console show me a 1 or 2.
How i get a whole number 12 ,22 in console?
#include <iostream>
int main()
{
using namespace std;
char a = 0;
cin >> a;
cout << a << endl;
return 0;
}
Here is console result.
12
1
C:\Users\kdwyh\source\repos\MyFirstProject\Debug\MyFirstProject.exe(프로세스 18464개)이(가) 종료되었습니다(코드: 0개).
이 창을 닫으려면 아무 키나 누르세요...
The reason I don't use int, string and something is because I want to get both number and Character in one variable.
So I want to see the results of combined numbers and character at the same time.
in that process i can't get a whole number.
#include <iostream>
using namespace std;
int index = 0;
constexpr int pagenum = 10;
void chapterlist(void);
void nextlist(void);
void beforelist(void);
void movechapter(char a);
int main(void)
{
char userin = 0;
bool toggle = 0;
cout << "결과를 볼 챕터를 고르시오." << endl;
chapterlist();
cout << "다음 페이지로 이동: n" << endl;
cin >> userin;
if (userin == 'n')
{
backflash:
while(toggle == 0)
{
nextlist();
cin >> userin;
if (userin == 'b')
{
toggle = 1;
goto backflash;
}
else if (userin == 'n')
continue;
else
{
system("cls");
movechapter(userin);
break;
}
}
while(toggle == 1)
{
beforelist();
cin >> userin;
if (userin == 'n')
{
toggle = 0;
goto backflash;
}
else if (userin == 'b')
continue;
else
{
system("cls");
movechapter(userin);
break;
}
}
}
else
{
system("cls");
movechapter(userin);
}
return 0;
}
void chapterlist(void)
{
int x = 0;
for (x = index + 1; x <= index + 10; x++)
cout << "Chapter." << x << endl;
}
void nextlist(void)
{
system("cls");
cout << "결과를 볼 챕터를 고르시오." << endl;
index = index + pagenum;
chapterlist();
cout << "다음 페이지로 이동: n" << endl;
cout << "이전 페이지로 이동: b" << endl;
}
void beforelist(void)
{
system("cls");
cout << "결과를 볼 챕터를 고르시오." << endl;
index = index - pagenum;
chapterlist();
cout << "다음 페이지로 이동: n" << endl;
cout << "이전 페이지로 이동: b" << endl;
}
void movechapter(char a)
{
cout << "선택한 Chapter." << a << "의 결과입니다." << endl;
}
In movechapter(), console show me a is 1 or 2, not 12, 22.
First, you have to understand what achar type is.
Character types: They can represent a single character, such as 'A' or '$'. The most basic type is char, which is a one-byte character. Other types are also provided for wider characters.
To simplify that, char can only hold one character.
Where as with your code, "12" is actually 2 separate characters, '1' and '2', and that's the reason it would not work.
Instead of declaring a as a char type, you could declare it as an int type, which is a type designed to hold numbers. So you would have:
int a = 0;
However, do note that int often has a maximum value of 2^31.
Or you could use std::string to store character strings. However, do note that if you wish to do any calculations to your string type, you would need to convert them to a number type first:
int myInt = std::stoi(myString);
Edit:
So I have re-checked your code after your update, there is nothing wrong with using std::string in your case. You can still check if user have input n or b by:
if (userin == "n")
Note that you would use double quotation mark, or "letter", around the content that you want to check.
On the other hand, you could use:
if(std::all_of(userin .begin(), userin.end(), ::isdigit))
To check if user have input a number.
Although char is just a number, it's presumed to mean "single character" here for input. Fix this by asking for something else:
int a = 0;
You can always cast that to char as necessary, testing, of course, for overflow.
You should be reading characters into a string, and then converting that string into an int. It would also probably make more sense to use something like getline() to read input, rather than cin >> a.
#include <string>
#include <iostream>
#include <stdexcept>
#include <stdio.h>
int main() {
std::string input_string;
/* note that there is no function that will convert an int string
to a char, only to an int. You can cast this to a char if needed,
or bounds check like I do */
int value;
while(1) {
getline(std::cin, input_string);
/* std::stoi throws std::invalid_argument when given a string
that doesn't start with a number */
try {
value = std::stoi(input_string);
} catch (std::invalid_argument) {
printf("Invalid number!\n");
continue;
}
/* You wanted a char, the max value of a `char` is 255. If
you are happy for any value, this check can be removed */
if (value > 255) {
printf("Too big, input a number between 0-255\n");
continue;
}
break;
}
printf("Number is %hhu\n", value);
}
This code is meant to detect REAL numbers from a string entered continuously by a user, and return the found real number as a double value . I was able to construct it to the point where it detects whole numbers, but if I try a decimal number it doesn't detect it. I think my error is within my isvalidReal() function, but I'm not sure how to move things around to get it to work.
#include <iostream>
#include <string>
#include <cctype>
using namespace std;
int main()
{
double getaReal(); //function prototype
int value;
cout << "Enter a number: ";
value = getaReal();
cout << "the number entered is a real number: " <<value << endl;
return 0;
}
bool isvalidReal(string str) //function to find real numbers.
{
int start = 0;
int i;
bool valid = true;
bool sign = false;
if (int(str.length()) == 0) valid = false; //check for empty string
if (str.at(0) == '-' || str.at(0) == '+') //check for sign
{
sign = true;
start = 1; //check for real num at position 1
}
if (sign && int(str.length()) == 1) valid = false; //make sure there's atleast 1 char after the sign
i = start;
while (valid && i<int(str.length()))
{
if (!isdigit(str.at(i))) valid = false; //found a non-digit character
i++; // move to next character
}
return valid;
}
double getaReal()
{
bool isvalidReal(string); //function declaration prototype
bool isnotreal = true;
string input;
while (isnotreal)
{
try
{
cin >> input; //accepts user input
if (!isvalidReal(input)) throw input;
}
catch (string e)
{
cout << "No real number has been detected, continue to\n enter string values: ";
continue; //continues user input
}
isnotreal = false;
}
return atof(input.c_str());
}