-
June 30th, 2017, 07:57 AM
#1
[RESOLVED] Issue with stream extraction
So if I want to validate a single character input I was trying something like that
Code:
char ch;
do {
cout << "\nPlease enter a single character :\n " << endl;
getline ( cin ,ch );
}while ( ch.length != 1 && cout <<" Wrong input ! " );
I want the user to enter a single character only if not it will tell him to reenter you see ?
Last edited by david16; June 30th, 2017 at 08:00 AM.
-
June 30th, 2017, 08:05 AM
#2
Re: join to piece of a program
Code:
}while ( ch.length() != 1 && cout <<" Wrong input ! " );
Once the syntax is correct, this will accept a single char terminated by a <newline>.
.length() is a member function of class string - not a member variable!
Last edited by 2kaud; June 30th, 2017 at 08:07 AM.
All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!
C++23 Compiler: Microsoft VS2022 (17.6.5)
-
June 30th, 2017, 08:24 AM
#3
Re: join to piece of a program
it worked but first time before even input it shows wrong input why ?
this is from cmd
Please enter a single character of your choice :
// didnt input yet !
Sorry Wrong input !
Please enter a single character of your choice :
-
June 30th, 2017, 08:45 AM
#4
Re: join to piece of a program
Code:
#include <iostream>
#include <string>
using namespace std;
int main()
{
string ch;
do {
cout << "\nPlease enter a single character :\n " << endl;
getline(cin, ch);
} while (ch.length() != 1 && cout << " Wrong input ! ");
}
Works for me OK.
Code:
c:\MyProgs>test12
Please enter a single character :
qq
Wrong input !
Please enter a single character :
q
c:\MyProgs>
All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!
C++23 Compiler: Microsoft VS2022 (17.6.5)
-
June 30th, 2017, 09:17 AM
#5
Re: join to piece of a program
Code:
int x;
do {
cout << "\nPlease enter number ( > 2 ) :\n " << endl;
cin >> x;
}while ( x < 2 && cout << "\nSorry wrong input!\n " );
string ch;
do {
cout << "\nPlease enter a single character :\n " << endl;
getline(cin, ch);
} while (ch.length() != 1 && cout << " Wrong input ! ");
try both together it will not work prorperly
Last edited by david16; June 30th, 2017 at 09:20 AM.
-
June 30th, 2017, 09:15 AM
#6
Re: join to piece of a program
I have another do while statement before this validation when I remove it everything is fine . But when I add it I get wrong input first then the message "please enter a single character "..... they are 2 separate do while why they are affect each other ?
-
June 30th, 2017, 09:23 AM
#7
Re: join to piece of a program
You are mixing stream extraction (>>) with getline(). >> doesn't remove the ending <newline> because >> ignores all white-space found before input. getline() obtains the data from the stream and removes the training <newline>. So if you have >> followed by getline(), the getline() gets the <newline> left over from the >> and sets the value to "" (empty) which has a length of 0. Hence the issue you are finding.
What you need to do is after the first do loop, remove the remaining <newline>. Try
Code:
cin.ignore(1000, '\n');
between the two do loops.
All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!
C++23 Compiler: Microsoft VS2022 (17.6.5)
-
June 30th, 2017, 09:30 AM
#8
Re: join to piece of a program
Oh I see now what's the problem . thx got it work fine now
-
June 30th, 2017, 11:17 AM
#9
Re: Issue with stream extraction
Last edited by 2kaud; June 30th, 2017 at 01:14 PM.
All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!
C++23 Compiler: Microsoft VS2022 (17.6.5)
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|