Restriction

hi guys, i have just one simple doubt i hope you can clear for me, i have a restriction here, and it goes through the whole vector verifying if there is a null value
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>
using namespace std;
int main() {
char number[20];
bool isNumber;
do{
isNumber = true;
cout<<"enter your number: ";
cin>>number;
for(int i = 0; number[i] != '\0'; i++){
if(!isdigit(number[i])){
isNumber = false;
cout<<"\nYour Number is Invalid\n";
break;
}
}
}while(!isNumber);
cout<<"\nThe Number you entered is: "<<numero;
cin.get();
return 0;
}


but if i enter for example '3 e', like that with the space, it stops, takes the 3 and adds the e to the next thing i enter. so usually then it crashes when the next variable i'm going to enter is an int.
i don't know if i explained myself perfectly jaja, i would appreciate if you help me.
cin >> doesn't read spaces, use getline instead :



and why would you need spaces ? numbers doesn't have spaces ?

~~~~~~~~~

Also i recommend to use std::string instead of fixed sized char array, but you have to use getline( cin, number ) instead and change number declaration to string number;

and #include <string>
Last edited on
Topic archived. No new replies allowed.