Hello.

I have a small problem in my home assignment. A neverending loop occurs, if user enters everything but digits, when I expect him to enter only digits.

How do I allow only int type variables to be entered by user?
The value user enters is the index number of node in linked list, which is deleted afterwards. If uder enters a,c,d... a neverending loop occurs later, when program tries to delete th n-th element in the list.

Posting the whole program is not needed I believe. The part I provide should be enough.

Thanks.

int number1;
int number2=5;

do{
cout<<"Which element do you want to delete?";
cin>>number1;
}while(number1<1 || number1>number2);

I don't think you can force input to be a digit. Look up the good() and ignore() stream member functions. Then, once you are familiar with them, modify your validation loop.

i know when you do web page programming there is a function called NaN---not a number but i dont think c++ has one. maybe you could do a if...else statement in your while loop. like if its a char...ignore it using cin.ignore(1)....else you let the program continue....but for the cin.ignore() function make sure you look it up first to find out its functionality before you use it just be safe

int number1;
int number2=100;

P:do{
cout<<"Which element do you want to delete?";
cin>>number1;

if (!cin)
{
cin.clear();
cin.ignore(100);
goto P;
}
}while(number1<1 || number1>number2);

I realise that using goto is a bad practice. I only learned about it a moment ago, and it does not seem like a good option. Anyway, this part of the code is still not working.

Now, when user enters a letter, program "freezes". There is no error message, it simply stops. I can keep entering letters/digits etc, and hit enter. Everything I write stays on the screen.

You'll want something more like

if (!cin.good()) {
  cin.clear();
  cin.ignore();
}

Obsidian how about you try the "isdigit()" function, it should make checking easier.

Obsidian how about you try the "isdigit()" function, it should make checking easier.

That only works on single chars. His variable is an int. As a result, if a non-digit character (such as 'a') is entered, you wind up with a corrupted input stream that needs to be detected and corrected.

That being said, here is another possibility to accept a single character that is a digit from 0-9:

char inputChar;
int number;
bool goodInput = false;

do {
  cout << "Enter a digit: ";
  cin >> inputChar;

  if (isdigit(inputChar)) {
    number = atoi(inputChar);
    goodInput = true;
  } else {
    cout << "Invalid input!" << endl;
    goodInput = false;
  }
} while(!goodInput)

isdigit() can only check 1 symbol at a time. If user enters abcd, I have to run isdigit() 3 times. It should check if 1st symbol is a digit, if 2nd symbol is a digit and if 3rd symbol is a digit.

I think I'd need a loop for that, which would "walk through" the string, starting from the 1st symbol. Loop would run n times, where n is the length of the string. My idea is using strlen() for that, however that does not work.

Compiler returns an error for this line:
cout<<strlen(number1);

I think that strlen() can only be used on char type strings? Is that right?

Correct, it can only be used on C-style strings (null-terminated char arrays). Looks like we overlapped a little, did you see my previous post?

Correct, it can only be used on C-style strings (null-terminated char arrays). Looks like we overlapped a little, did you see my previous post?

I saw it, my previous post was answer to the one who suggested using isdigit().

Your code results in 2 compiler errors for me:
Error: jauns.cpp(19,29):Cannot convert 'int' to 'const char *'
Error: jauns.cpp(19,29):Type mismatch in parameter '__s' in call to 'atoi(const char *)'

#include <conio.h>
#include <iostream.h>
#include <stdio.h.>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>

void main(){

char inputChar;
int number;
bool goodInput = false;
 
do {
  cout<<"Enter a digit: ";
  cin>>inputChar;

  if(isdigit(inputChar)) {
// this is the line giving me trouble...
    number = atoi(inputChar);
    goodInput = true;
  }else {
    cout << "Invalid input!" << endl;
    goodInput = false;
  }
}while(!goodInput);

cout<<number;
getch();
}

Yeah, I goofed. Try passing it as a reference.

number = atoi([B]&[/B]inputChar);

Although, I'm surprised that's your only error. Your header names are not standard and you aren't resolving the std namespace properly.

I think I have fixed it, so there is no need for anyone to reply at the moment. I'll see if my current solution orks, if it does, I'll post my final code here later :)

Thanks all, and especially Fbody.

you can use "c_str();" to convert to a C string

you can use "c_str();" to convert to a C string

The function c_str() is a member function of the STL std::string class. It is used to pull a C-style string out of an std::string. There are no std::strings in the program. As such, it is not available to anything in the program.

I suppose that if you use getline() to store the input to an std::string, you could use c_str() in conjunction with strtok() to tokenize the string then process the tokens. As the program is written though, c_str() is not a possibility.

Hmmm..... I just realized that I may have crossed thoughts with another similar thread. Disregard the second part of my previous post, but the first part still applies.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.