Click here to Skip to main content
15,909,530 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
AnswerRe: RegisterDeviceNotification in dbt.h undeclared identifier?? Pin
Richard MacCutchan24-Jul-13 21:24
mveRichard MacCutchan24-Jul-13 21:24 
GeneralRe: RegisterDeviceNotification in dbt.h undeclared identifier?? Pin
Vaclav_25-Jul-13 7:17
Vaclav_25-Jul-13 7:17 
GeneralRe: RegisterDeviceNotification in dbt.h undeclared identifier?? Pin
Jochen Arndt25-Jul-13 7:43
professionalJochen Arndt25-Jul-13 7:43 
GeneralRe: RegisterDeviceNotification in dbt.h undeclared identifier?? SOLVED Pin
Vaclav_26-Jul-13 5:50
Vaclav_26-Jul-13 5:50 
AnswerRe: RegisterDeviceNotification in dbt.h undeclared identifier?? SOLVED Pin
Richard Andrew x6426-Jul-13 12:26
professionalRichard Andrew x6426-Jul-13 12:26 
GeneralRe: RegisterDeviceNotification in dbt.h undeclared identifier?? Pin
Richard MacCutchan25-Jul-13 9:14
mveRichard MacCutchan25-Jul-13 9:14 
QuestionProblem with loop in C++ Pin
Kripa Khanal24-Jul-13 5:40
Kripa Khanal24-Jul-13 5:40 
AnswerRe: Problem with loop in C++ Pin
pasztorpisti24-Jul-13 7:21
pasztorpisti24-Jul-13 7:21 
The problem is that in case of a wrong answer you leave garbage in your iostream plus the error flags of the iostream may remain set.
A slitly better but still buggy version:
C++
#include<iostream> 
#include<ctime>
#include <limits>
using namespace std; 
int funccccc() 
{ 
    int num1=0; 
    int num2=0; 
    int correctanswer=0; 
    int useranswer=0; 
    srand(static_cast<int>(time(0))); 
    for (int x=1; x<6; x++) 
    { 
        num1=1+rand()%(100-1+1); 
        num2=1+rand()%(10-1+1); 
        correctanswer= num1+num2; 
        // The inner loop is needed if we want to ask the same question after an invalid input value.
        for (;;)
        {
            cout<<"What is the sum of "<<num1<<" and "<<num2<<"?"; cin>>useranswer;
            if (!cin.fail())
                break;
            cin.clear();
            cin.ignore(std::numeric_limits<streamsize>::max(),'\n');
            cout << "Invalid value!" << endl;
        }

        if (useranswer==correctanswer) 
            cout<<"Excellent! Correct Answer" <<endl;
        else 
            cout<<"Sorry!the correct answer is: "<<correctanswer; cout<<endl<<endl; 
    }

    system("pause"); 
    return 0; 
}

The bug in this version is the following:
Lets say you answer 5.6 for the first question. In this case your first cin>>useranswer statement will read out "5" into the useranswer by ignoring everything from the input stream from the '.' character. It evaluates the first question with your answer '5' and then it asks the next question. Since we still have characters in the input stream the cin>>useranswer statement of the next question tries to read it as an integer. Unfortunately it fails and our error handler clears the error flags and discards everything from the input stream till the next newline character. From the next question everything is OK. If you simply enter a garbage string that doesn't start with a digit then it works perfectly. The bug of this solution occurs if you enter a garbage string whose first character is a digit like "5sfldfhwei".

You could correct this by always reading in everything till the next newline, stripping the spaces from the left and right side of the string and trying to interpret it as it is without ignoring any characters in the string. I'm not a big iostream fan/user and I don't know how to interpret a string as an integer with iostream without ignoring any characters from the right side of the string.
GeneralRe: Problem with loop in C++ Pin
Kripa Khanal25-Jul-13 12:06
Kripa Khanal25-Jul-13 12:06 
GeneralRe: Problem with loop in C++ Pin
pasztorpisti25-Jul-13 22:59
pasztorpisti25-Jul-13 22:59 
AnswerRe: Problem with loop in C++ Pin
Member 1014853124-Jul-13 16:13
Member 1014853124-Jul-13 16:13 
QuestionSerial Port Functor Pin
Jonathan Davies24-Jul-13 1:51
Jonathan Davies24-Jul-13 1:51 
AnswerRe: Serial Port Functor Pin
pasztorpisti24-Jul-13 3:29
pasztorpisti24-Jul-13 3:29 
AnswerRe: Serial Port Functor Pin
Chris Losinger24-Jul-13 10:26
professionalChris Losinger24-Jul-13 10:26 
GeneralRe: Serial Port Functor Pin
Jonathan Davies24-Jul-13 10:36
Jonathan Davies24-Jul-13 10:36 
QuestionGetModuleFileName and case sensitivity Pin
vikramlinux23-Jul-13 23:19
vikramlinux23-Jul-13 23:19 
AnswerRe: GetModuleFileName and case sensitivity Pin
«_Superman_»23-Jul-13 23:27
professional«_Superman_»23-Jul-13 23:27 
GeneralRe: GetModuleFileName and case sensitivity Pin
vikramlinux23-Jul-13 23:32
vikramlinux23-Jul-13 23:32 
GeneralRe: GetModuleFileName and case sensitivity Pin
vikramlinux23-Jul-13 23:36
vikramlinux23-Jul-13 23:36 
GeneralRe: GetModuleFileName and case sensitivity Pin
Richard MacCutchan24-Jul-13 0:25
mveRichard MacCutchan24-Jul-13 0:25 
AnswerRe: GetModuleFileName and case sensitivity Pin
SajeeshCheviry24-Jul-13 0:24
SajeeshCheviry24-Jul-13 0:24 
GeneralRe: GetModuleFileName and case sensitivity Pin
vikramlinux24-Jul-13 1:23
vikramlinux24-Jul-13 1:23 
SuggestionRe: GetModuleFileName and case sensitivity Pin
Richard MacCutchan24-Jul-13 4:56
mveRichard MacCutchan24-Jul-13 4:56 
GeneralRe: GetModuleFileName and case sensitivity Pin
vikramlinux24-Jul-13 16:56
vikramlinux24-Jul-13 16:56 
GeneralRe: GetModuleFileName and case sensitivity Pin
Richard MacCutchan24-Jul-13 21:13
mveRichard MacCutchan24-Jul-13 21:13 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.