Nested switch gives error and indicates virus (trojan) - c++

Trying to make a program using switch case (nested switch) my system alerted me that my program has a virus (trojan). How is it even possible? I am new to programming (complete novice) so I would be grateful for any help.
The task - to make automated telephonic reply system based upon requirements (just something I wanted to try).
#include<iostream>
using namespace std;
void customer_service()
{
cout<<"Kindly wait for our employees to contact you";
}
void feedback()
{
cout<<"Kindly record your feedback after the beep";
}
void offer()
{
cout<<"You are entitled to accept our one-time offer. You will be directed to one of our employees shortly\n";
}
void satisfied()
{
cout<<"Thanks a lot for calling. Have a great day ahead";
}
int main()
{
int input,yes_no;
cout<<"\nPress 1 if you would want to directly contact our employee\n";
cout<<"\nPress 2 if you wan to give a feedback\n";
cout<<"\nPress 3 if ypu would want to know about our offers\n";
cout<<"\nPress 4 if you are satisfied with our service\n";
cout<<"\nKindly press the required key\n";
cin>>input;
switch (input)
{
case 1:
customer_service();
break;
case 2:
feedback();
break;
case 3:
offer();
cout<<"Would you like to accept our one time offer? You will get a 50% decrease in tariff";
cin>>yes_no;
switch (yes_no)
{
case 1:
cout<<"Congratulations! You have won our one time offer";
break;
default:
cout<<"Guess you didn't like our offer";
break;
}
break;
case 4:
satisfied();
break;
default:
cout<<"Kindly press either one of '1, 2, 3 or 4' keys. Thankyou.";
}
cin.get();
return 0.00;
}
This is the indication of Trojan and the program not executing

It's a false positive.
You may be able to help the situation by initialising your variables. As it is, you do not check that reading into yes_no succeeded, so your program has undefined behaviour. That could make your AV think that you are trying to write a memory exploit.
Otherwise, get better AV!

Some anti virus programs simply have false positives. Just whitelist in this case or get another anti virus.
OR your toolchain itself is infected and you compile bad stuff into your programs (then it's time to clean up your OS)

There is nothing wrong with your code.
Programs like 360 total security are anti-malware products that are designed to run on your mother's machine. They are not appropriate on a programmer's machine. They deal poorly with an executable file that appears from nowhere. Uninstall and consider something less aggressive, Windows Defender for example.

Nested switch cases will work work for sure.
First try like instead of using second switch case use if else condition if still it shows TROJAN thing then its a problem with your compiler.

Related

Can I get character from keyboard without pausing a program

I'm working on a little project to improve my coding skills and I have a problem. I'm doing a console version of Flappy Bird. So i have a map which is a two-dimensional array of chars and this map have to move to the left. I am moving all elements of an array one place to the left and after that, clearing console and showing moved map. And here is problem, map has to move constantly but player have to control a bird while map is moving. I wanted to use _getch() but it pausing a program. A question is: Can i read a keyboard input without pausing program? I mean that the map will still moving and when i press for example Space in any moment the bird position will change. I'm working on Windows 10
Even if beginners hope it to be a simple operation, inputting a single character from the keyboard is not, because in current Operating Systems, the keyboard is by default line oriented.
And peeking the keyboard (without pausing the program) is even harder. In a Windows console application, you can try to use functions from user32, for example GetAsyncKeyState if you only need to read few possible keys: you will know if the key is currently pressed and whether if was pressed since the last call to GetAsyncKeyState.
But beware: these are rather advanced system calls and I strongly advise you not to go that way if you want to improve your coding skills. IMHO you'd better learn how to code Windows GUI applications first because you will get an event loop, and peeking for events is far more common and can be used in real world applications. While I have never seen a real world console application trying to peek the keyboard. Caveat emptor...
Including conio.h
you can use this method:
#define ARROW_UP 72
#define ARROW_DOWN 80
#define ARROW_LEFT 75
#define ARROW_RIGHT 77
int main(){
int key;
while( true ){
if( _kbhit() ){ // If key is typed
key = _getch(); // Key variable get the ASCII code from _getch()
switch( key ){
case ARROW_UP:
//code her...
break;
case ARROW_DOWN:
//code her...
break;
case ARROW_LEFT:
//code her...
break;
case ARROW_RIGHT:
//code her...
break;
default:
//code her...
break;
}
}
}
return 0;
}
The upper code is an example, you can do this for any key of keyboard. In this sites you can find the ASCII code of keys:
http://www.jimprice.com/jim-asc.shtml#keycodes
https://brebru.com/asciicodes.html
Say me if it has help you!

Is there any function available to transfer the flow in c++ programming alternative to while or do while loop?

I am new to programming. & generally used to do programming on weekends. While working on a mini ATM project the problem arrives when i need to transfer the flow of the program back to up to the first line. i have already written the code of 1256 line so i can't re-structured it for while or do while loop.I searched for it a lot in the online portals but couldn't found a satisfactory results. My question is that is there any in-build function or way available for that cause.
my first line was.std::cout<<"Wlcome to your account \n";
Then my selecting option.
std::cout<<"press 12 to go to main manue \n";
that was my else if statement from where i want to send back my flow towards the first line.
else if (in.amount==12) { }
what could i write in that brackets to send back the flow of program to first line and the screen show's me agian "Welcome to your account"
i have already written the code of 1256 line so i can't re-structured it for while or do while loop.
Why not? You could just wrap a while loop around the whole thing.
That said, there is a way to do exactly what you're asking for: goto.
First you need to label one of your statements. For example:
int main() {
the_beginning:
std::cout << "Welcome to your account\n";
...
}
Then you can do goto the_beginning; to transfer control to the statement labeled the_beginning.
See goto on cppreference for more information and examples.
There is no need for a goto (which is a very bad practice in a high-level language). You can simply wrap your whole function in an infinite loop:
You had:
void foo()
{
// code
// you want to restart here
// you want to quit here
// code
}
You will have:
void foo()
{
for(;;)
{
// code
// you want to restart here
continue;
// you want to quit here
break;
// code
break; // if you want to terminate at the end;
}
}

do-while doesn't quit on first 'q'

So I'm working on a simple little text-based game in D to gain some experience working with the language. Here is a do-while loop that I'm currently struggling with:
do{
writeln("a. Advance 1 year\tc. Advance 10 years\tq. Quit");
writeln("b. Advance 5 years\td. Modify faction");
input = chomp(stdin.readln());
switch(input){
...
default:
break;
}
writeln(input[0]);
}while(input[0] != 'q');
Now the problem I'm running into is that when I hit q and enter the loop doesn't exit. It just keeps going. But then after the first time q is input, another q will terminate the loop. The writeln is in there as a sanity check, and it prints out the characters I type in exactly as typed. I feel like I'm going crazy, but it's probably just a simple type-o or something you guys will spot instantly. Nothing in the switch statement modifies 'input'.
EDIT: Okay some people have been asking to see all of the code. Here it is: http://pastebin.com/A7qM5nGW
When I said nothing in the switch modified input, it was to hide the fact I hadn't written anything in the switch yet. I've been trying to get the quit part to work right before adding the more complicated stuff. Also, here's a sample file for what I run it on: http://pastebin.com/4c2f4Z5N
Okay my friend found it. It has nothing to do with the while loop itself. I briefly forgot that args[0] is the name of the program. So it's actually running through the parent loop once with nothing, then actually quitting, and then running through the appropriate loop. It was fixed by making the parent loop like so...
foreach(filename; args[1..$]){
...
do{
...
while(input[0] != 'q');
}
as opposed to:
foreach(filename; args){
etc...

PDCurses KEY_ENTER does not work

Lets start with what my code looks like then I will explain my problem:
int main {
char ch; //Stores key presses
initscr();
raw();
nonl();
keypad(stdscr, TRUE);
noecho();
//Some code
ch = getch();
switch (ch) {
case KEY_UP:{
//Code that works
break;
}
case KEY_ENTER:{
//Some code- that doesn't work problem being the above
break;
}
//Other case statements
}
Now the problem:
The problem I run into if you haven't already worked it out is that when ever I press the enter/return key on my keyboard absolutely nothing happens.
I have tried changing the KEY_ENTER to '\n' - didn't work - even changed the char ch which when through multiple iterations including int and wchar_t.
All to no avail, and before you say search for answers and send me packing my bags to go onto a perilous adventure through every corner of the interwebs, I have already tried that, if I hadn't I wouldn't have ventured here, in search of aid.
So now my search has brought me here and I ask of you - the lovely people of the interwebs - to help me in my search of the answer I have been looking for
And to who ever may be valiant enough to answer it I give you my up most gratitude and thanks
Try case '\r':. (For good measure, you could do case '\r': case '\n': case KEY_ENTER:, as is basically done in testcurs.c, to capture all possibilities.) The call to nonl() is why you're getting '\r' instead of '\n'.
As for KEY_ENTER, my only excuse is that it's marked "not reliable" in the PDCurses comments. I could pretend that it's meant to represent the keypad's "Enter" key, rather than the key usually marked "Return" in the main part of the keyboard... except that PDCurses also has PADENTER, specifically for that purpose. In truth, like a lot of things in PDCurses, the reason KEY_ENTER is there, and defined the way it is, is a bit of a historical mess.

Platform-independent detection of arrow key press in C++

In a C++ console program, I've found how to detect and arrow key on Windows, and I've found a lot of other stuff that had nothing to do with the question (despite what I thought were good search terms), but I want to know if there is a platform-independent way to detect an arrow key press. A decent second place for this would be how to detect arrow key press in unix and mac. Code fragments would be appreciated.
There's no cross platform way to do it because it's not defined by either C or C++ standards (though there may be libraries which abstract away the differences when compiled on different platforms).
I believe the library you are looking for on POSIX boxes is curses, but I've never used it myself -- I could be wrong.
Keep in mind that it's entirely possible the console program (i.e. gnome-terminal or konsole or xterm) has monopolized the use of those keys for other functions.
As Billy said, there is no standard cross-platform way to do it.
Personally I use this (game-oriented) library for all inputs, cross-platform win/linux/mac : http://sourceforge.net/projects/wgois/
You can do this cross-platform by using SDL2.
Example code:
#include <SDL2/SDL.h>
int main()
{
SDL_Event event;
SDL_PollEvent(&event);
if(event.type == SDL_KEYDOWN)
{
// Move centerpoint of rotation for one of the trees:
switch(event.key.keysym.sym)
{
case SDLK_UP:
// do something
break;
case SDLK_DOWN:
// do something
break;
case SDLK_LEFT:
// do something
break;
case SDLK_RIGHT:
// do something
break;
case SDLK_ESCAPE:
// do something
return 0;
default:
break;
}
}
return 0;
}