[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 202
  • Last Modified:

Reading from a command window

Hi,

I need to get a Yes or No from the user, via a command window interface.  Anyone know the code to do this?

Thanks,
Bob
0
ba272
Asked:
ba272
  • 5
  • 3
  • 2
2 Solutions
 
AxterCommented:
Hi ba272,
> >I need to get a Yes or No from the user, via a command window interface.  
> >Anyone know the code to do this?

#include <iostream>
#include <string>
using namespace std;

string Answer;
cout << "Enter yes or no" << endl;
cin >> Answer;

David Maisonave :-)
Cheers!
0
 
AxterCommented:
Sorry, I misread your question.

Try using AfxMessageBox or MessageBox API function
0
 
jkrCommented:
You could just use

#include <string.h>
#include <iostream>
#include <string>
using namespace std;
//...

string strResponse;
cout << "Enter 'Yes' or 'No'" << endl;
cin >> strResponse;

if (!_stricmp(strResponse.c_str(), "Yes")) cout << "Input was YES" << endl;
if (!_stricmp(strResponse.c_str(), "No")) cout << "Input was NO" << endl;



0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
AxterCommented:
Example code:

#include <windows.h>


int main(int argc, char* argv[])
{
    if (IDOK == MessageBox(NULL, "Enter Click Yes Or No", "Title Yes No", MB_YESNO))
    {
        //Click yes
    }
    else
    {
        //Clicked no
    }
    return 0;
}

0
 
AxterCommented:
FYI:
You can use IDYES for the comparisson, but IDOK will do the same job.

if (IDYES  == MessageBox(NULL, "Enter Click Yes Or No", "Title Yes No", MB_YESNO))
0
 
ba272Author Commented:
Thanks, I actually needed you first answer, David.  And thanks for the addition jkr.  But the version of the program i'm working with still uses header files and include statements.  

How different will the code:

string Answer;
cout << "Enter yes or no" << endl;
cin >> Answer;

be when using it on an older version of C++?


Thanks,
Bob
0
 
jkrCommented:
>>How different will the code be when using it on an older version of C++?

As long as it is ANSI C++ and not some stone age compiler, there won't be any differences. Yet you could still resort to the C version:

char acResponse[255];

printf("Enter Yes or No:");
scanf("%255.255s", acResponse);

if (!_stricmp(acResponse, "Yes")) printf( "Input was YES");
if (!_stricmp(acResponse, "No")) printf( "Input was NO");
0
 
AxterCommented:
>>be when using it on an older version of C++?

The above code will work with the older version of C++.

Which compiler and what verison of the compiler?
0
 
ba272Author Commented:
It's the latest actually, but the project I am updated was older, and I didn't expect the "using" statement to work.  I'll give it a try.  Thanks.
0
 
jkrCommented:
>>I didn't expect the "using" statement to work

It will - VC6 e.g. is from '98 and for sure recognizes it.
0

Featured Post

Prep for the ITIL® Foundation Certification Exam

December’s Course of the Month is now available! Enroll to learn ITIL® Foundation best practices for delivering IT services effectively and efficiently.

  • 5
  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now