I am working on a program that scans a host and I ve run in to a simple problem I cant figure out. When it asks what type of scan to perform and you say number 2 it will go to the number 1 type of scan. Here is the code:

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

int main()
{
    string ping = "ping";
    string ip_address;
    string ipai;
    string ping_ip;
    string ping_ip_intense;
    int rd;
    int number_of_scan;
    string scan;
    string intense_scan;
    string light_scan;
    
    
    cout<<"1. Regular Scan"<<endl;
    cout<<"2. Intense Scan"<<endl;
    cout<<"3. Light Scan"<<endl;
    cout<<"\n\n\n"<<endl;
    
    cout<<"Please enter the number of the scan you would like to perform: "<<endl;
    cin>>number_of_scan;

    if (number_of_scan = '1'){
    cout<<"You are going to perform the Regular Scan"<<endl;
    cout<<"Please enter the IP address or domain of the host: "<<endl;
    cin>>ip_address;
    ping_ip= ping+" "+ip_address;
    system(ping_ip.c_str());
}
    
    if (number_of_scan = '2'){
    cout<<"You are goin to perform the Intense Scan"<<endl;
    cout<<"Please enter the IP address or domain of the host: "<<endl;
    cin>>ipai;
    rd = rand()% 83 + 107;
    cout<<"Estimated time(seconds): "<<rd<<endl;
    ping_ip_intense="ping "+ipai+" -n 20 -l 60 -w 10000";
    system(ping_ip_intense.c_str());
}
system("pause");
return 0;
}

Thank You.

Member Avatar for iamthwee

Loose the '' around the numbers. i.e '2' becomes just 2

Oh and the = should actually be == in the if statement.

To explain what Wee is telling you...

Loose the '' around the numbers. i.e '2' becomes just 2

You don't need to loosen anything, you need to lose something... :icon_rolleyes:
'2' is the ASCII character, 2 is the number. You need to compare the number 2, not the character. Same with '1'

Oh and the = should actually be == in the if statement.

= is the assignment operator, == is the comparison operator. Therefore, if (number_of_scan = '1'){ actually assigns the character '1' to the variable.

Also, see this about formatting your code so it's easier to read.

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.