I want to add the check-in my c++ code that user can not enter not integral values in reg. If he inputs, he is prompted again. I saw the solutions in the stack overflow that was of 2011 (How to check if input is numeric in C++). Is there some modern or good way now or is it same?
I tried using ifdigit() in ctype.h
// Example program
#include <iostream>
#include <ctype.h>
using namespace std;
int main()
{
int x;
cout<<"Type X";
cin>>x;
if(!isdigit(x))
{
cout<<"Type Again";
cin>>x;
}
}
but it didnt worked
here is my actual problem where I want to add check.
cout << "Type Reg # of Student # " << i + 1 << endl;
do
{
cin >> arr[i][j];
} while (arr[i][j] < 999 || arr[i][j] > 9999);
where i and j are in dec. in for loop. I just want to add check that input is not string or something like this. Cant rely on 2011 answer
Check out the below example.
All the magic happens inside to_num(), which will handle white space before and after the number.
#include <iostream>
#include <sstream>
#include <string>
#include <tuple>
auto to_num(const std::string& s)
{
std::istringstream is(s);
int n;
bool good = (is >> std::ws >> n) && (is >> std::ws).eof();
return std::make_tuple(n, good);
};
int main()
{
int n;
bool good;
std::cout << "Enter value: ";
for(;;)
{
std::string s;
std::getline(std::cin, s);
std::tie(n, good) = to_num(s);
if(good) break;
std::cout << s << " is not an integral number" << std::endl;
std::cout << "Try again: ";
}
std::cout << "You've entered: " << n << std::endl;
return 0;
}
Explanation of what's going on inside to_num():
(is >> std::ws >> n) extracts (optional) leading white space and an integer from is. In the boolean context is's operator bool() will kick in and return true if the extraction was successful.
(is >> std::ws).eof() extracts (optional) trailing white space and will return true if there is no garbage at the end.
UPDATE
Here is a slightly cleaner version that uses Structured binding declaration and Class template argument deduction available in c++17:
#include <iostream>
#include <sstream>
#include <string>
#include <tuple>
auto to_num(const std::string& s)
{
std::istringstream is(s);
int n;
bool good = (is >> std::ws >> n) && (is >> std::ws).eof();
return std::tuple(n, good); // look ma, no make_tuple
};
int main()
{
std::cout << "Enter value: ";
for(;;)
{
std::string s;
std::getline(std::cin, s);
auto [n, good] = to_num(s); // structured binding
if(good)
{
std::cout << "You've entered: " << n << std::endl;
break;
}
else
{
std::cout << s << " is not an integral number" << std::endl;
std::cout << "Try again: ";
}
}
return 0;
}
If you properly handle errors, you'll end up with a prompt / input loop that looks something like this:
#include <iostream>
#include <limits>
int getInput() {
while (true) {
std::cout << "Please enter a number between 80 and 85: ";
int number = 0;
std::cin >> number;
std::cout << "\n";
if (std::cin.eof()) {
std::cout << "Unexpected end of file.\n";
std::cin.clear();
continue;
}
if (std::cin.bad() || std::cin.fail()) {
std::cout << "Invalid input (error reading number).\n";
std::cin.clear();
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
continue;
}
if (number < 80 || number > 85) {
std::cout << "Invalid input (number out of range).\n";
continue;
}
return number;
}
// unreachable
return 0;
}
int main() {
int number = getInput();
std::cout << number << std::endl;
}
We can omit the range check if we don't need it.
We handle std::cin.eof() (e.g. user presses ctrl+Z on Windows) separately from the other conditions, since for eof there's nothing to ignore.
This follows the standard C++ stream behavior for whitespace and number conversion (i.e. it will accept inputs with extra whitespace, or inputs that only start with numeric values).
If we want more control over what we want to accept, or don't want conversion to depend on the locale, we have to use std::getline to read input from std::cin, and then do the string conversion ourselves (either with std::stringstream as in Innocent Bystander's answer, or using std::strtol, or std::from_chars).
I cannot figure out why my getchar() function is not working the way I want it to work. I am getting 10 not 2. Please take a look.
Main():
#include <cstdlib>
#include <iostream>
#include <fstream>
using namespace std;
int main() {
int var, newvar;
cout << "enter a number:" << endl;
cin >> var;
newvar = getchar();
cout << newvar;
return 0;
}
Here is my output:
enter a number:
220
10
Ultimately though I need to be able to distinguish between a '+' '-' or letter or number.
This is maybe not the cleanest way to do it but you can get every char one by one :
#include <iostream>
using namespace std;
int main()
{
int var;
cout << "enter a number:" << endl;
cin >> var;
std::string str = to_string(var);
for(int i=0; i < str.length();++i)
cout << str.c_str()[i] << endl;
return 0;
}
If you enter for example: "250e5" it will get only 250 and skip the last 5.
Edit:
This is just a simple parser and does not do any logic.
If you want to make a calculator I would recommend you to look at what Stroustrup did in his book the c++ programming language.
int main()
{
string str;
cout << "enter a number:" << endl;
cin >> str;
for(int i=0; i < str.length();++i) {
char c = str.c_str()[i];
if(c >= '0' && c <= '9') {
int number = c - '0';
cout << number << endl;
}
else if(c == '+') {
// do what you want with +
cout << "got a +" << endl;
} else if(c == '-')
{
// do what you want with -
cout << "got a -" << endl;
}
}
return 0;
}
This program I am making has the user enter a string and a character and then displays the number of times that character appears in the string. For some reason every time I run this program it always says the character appears 0 times in the string. I need some help identifying the problem and fixing it. Thanks!
#include <cstdlib>
#include <iostream>
#include <string>
#include <cctype>
using namespace std;
int main(int argc, char *argv[])
{
string input;
char character;
int charCount = 0;
cout << "Enter a string:" << endl;
getline(cin, input);
cout << "Enter a character:" << endl;
cin >> character;
int i = input.find(character);
while (i < 0)
{
charCount++;
i = input.find(character, (i + 1));
}
cout << character << " appears in the string, " << input << ", " << charCount << " times." << endl;
system("PAUSE");
return EXIT_SUCCESS;
}
Your issue is in the loop condition
int i = input.find(character); // if the character is in the string, it will return a number i > 0
while (i < 0) // will not enter loop
{
charCount++;
i = input.find(character, (i + 1));
}
Also, charCount is going to increase with every character you encounter in the string based on this logic.
I need to write a program that checks if the user-provided first and last names are correctly typed. The program needs to validate that only the first letter of each name part is uppercase.
I managed to write code that checks the first character of the input. So I have a problem when for example "JOHN" is entered.
A correct input would be for example "John Smith".
Here's the code:
#include <iostream>
#include <string>
using namespace std;
int main ()
{
std::string str;
cout << "Type First Name: ";
cin >> str;
if(isupper(str[0]))
{
cout << "Correct!" <<endl;
}
else
{
cout << "Incorrect!" <<endl;
}
system("pause");
return 0;
}
The simplest thing you can do is to use a for/while loop. A loop will basically repeat the same instruction for a number of n steps or until a certain condition is matched.
The solution provided is pretty dummy, if you want to read the first name and last name at the same time you will have to spit the string via " " delimiter. You can achieve this result using strtok() in C/C++ or with the help of find in C++. You can see some examples of how to split here.
You can easily modify your code to look like this:
#include <iostream>
#include <string>
#include <vector>
using namespace std;
int main()
{
std::string str;
std::vector<std::string> data = { "First", "Last" };
int j;
for (int i = 0; i < 2; i++) {
cout << "Type " << data[i] << " Name: ";
cin >> str;
if (isupper(str[0])) {
for (j = 1; j < str.size(); j++) {
if (!islower(str[j]))
{
cout << "InCorrect!" << endl;
break; // Exit the loow
}
}
if(j==str.size())
cout << "Correct!" << endl;
}
else {
cout << "InCorrect!" << endl;
}
}
system("pause");
return 0;
}
I'm needing help in adding commas to the number the user enters, some guidance or help would be appreciated. So far I have it where i store the first three digits and the last six digits and then simply format it.
#include<iostream>
using namespace std;
int main ( int argc, char * argv[] )
{
unsigned long long userInput;
int fthreeDigit;
cout << "Enter a long long number: " << endl;
cin >> userInput;
fthreeDigit = ( userInput / 1000 );
userInput %= 1000;
cout << "Your Number: " << fthreeDigit << "," << userInput << endl;
system("pause");
return 0;
}
Is this what you need? The locale will do this for you correctly.
#include <iostream>
using namespace std;
int main ( int argc, char * argv[] )
{
unsigned long long userInput;
int fthreeDigit;
cout << "Enter a long long number: " << endl;
cin >> userInput;
std::cout.imbue(std::locale(""));
std::cout << userInput << std::endl;
return 0;
}
EDIT:
I have two solutions. first without playing with numbers (recommended) and second (division).
first solution is:
#include <cstdlib>
#include <iostream>
#include <locale>
#include <string>
using namespace std;
struct my_facet : public std::numpunct<char>{
explicit my_facet(size_t refs = 0) : std::numpunct<char>(refs) {}
virtual char do_thousands_sep() const { return ','; }
virtual std::string do_grouping() const { return "\003"; }
};
/*
*
*/
int main(int argc, char** argv) {
cout<<"before. number 5000000: "<<5000000<<endl;
std::locale global;
std::locale withgroupings(global, new my_facet);
std::locale was = std::cout.imbue(withgroupings);
cout<<"after. number 5000000: "<<5000000<<endl;
std::cout.imbue(was);
cout<<"and again as before. number 5000000: "<<5000000<<endl;
return 0;
}
before. number 5000000: 5000000
after. number 5000000: 5,000,000
and again as before. number 5000000: 5000000
RUN SUCCESSFUL (total time: 54ms)
and second (not recommended) is :
double f = 23.43;
std::string f_str = std::to_string(f);
or this
int a = 1;
stringstream ss;
ss << a;
string str = ss.str();
Then you can use string::substr() string::find() string::find_first_of() and similar methods to modify and format your string.
a similar topic
If you really want (have to) divide: (I think my version is cleaner & more efficient than the others)
unsigned long long userInput;
std::stringstream ss,s0;
std::string nr;
std::cout << "Enter a long long number: " << std::endl;
std::cin >> userInput;
int input=userInput;
int digits;
while(input>999){
input=input/1000;
digits=userInput-input*1000;
int mdigits=digits;
while(mdigits<100){s0<<"0";mdigits*=10;}
std::string s=ss.str();
ss.str("");
ss<<","<<s0.str()<<digits<<s;
userInput=input;
s0.str("");
}
std::string sf=ss.str();
ss.str("");
ss<<input<<sf;
std::cout << "Your Number: " << userInput << ";" << digits <<";"<<ss.str()<<std::endl;
Enter a long long number: 12345678 Your Number: 12;345;12,345,678
Here is the brute force but may be easiest to understand way to get every thousand digits with the help of a vector.
#include<iostream>
#include <algorithm>
#include <vector>
using namespace std;
int main ( int argc, char * argv[] )
{
long long userInput;
int fthreeDigit;
cout << "Enter a long long number: " << endl;
cin >> userInput;
vector <int> res; //use vector to store every 3 digits
while (userInput !=0)
{
fthreeDigit = userInput %1000;
res.push_back(fthreeDigit);
userInput = userInput / 1000 ;
}
std::reverse(res.begin(), res.end());
for (size_t i = 0; i < res.size()-1; ++i)
{
if (res[i] ==0)
{
cout << "000"<<",";
}
else
{
cout << res[i] << ",";
}
}
if (res[res.size()-1] == 0)
{
cout << "000";
}
else{
cout << res[res.size()-1];
}
cout <<endl;
cin.get();
return 0;
}
I tested this code with the following case:
Input: 123456 Output: 123,456
Input: 12 Output: 12
Input: 12345 Output: 12,345
Input: 1234567 Output: 1,234,567
Input: 123456789 Output: 123,456,789
Input: 12345678 Output: 12,345,678
I guess this is what you want according to your response to comments.
You could do this:
#include <iostream>
#include <string>
using namespace std;
string commify(unsigned long long n)
{
string s;
int cnt = 0;
do
{
s.insert(0, 1, char('0' + n % 10));
n /= 10;
if (++cnt == 3 && n)
{
s.insert(0, 1, ',');
cnt = 0;
}
} while (n);
return s;
}
int main()
{
cout << commify(0) << endl;
cout << commify(1) << endl;
cout << commify(999) << endl;
cout << commify(1000) << endl;
cout << commify(1000000) << endl;
cout << commify(1234567890ULL) << endl;
return 0;
}
Output (ideone):
0
1
999
1,000
1,000,000
1,234,567,890
// Accepts a long number, returns a comma formatted string
CString num_with_commas(long lnumber)
{
CString num;
num.Format(%d",lnumber);
if(num.GetLength() > 3) {num.Insert(num.GetLength()-3, ',');}
if(num.GetLength() > 7) { num.Insert(num.GetLength()-7, ','); }
if (num.GetLength() > 12) { num.Insert(num.GetLength()-12, ','); }
return(num);
}