How to use if statment?

closed account (G8hb7k9E)
I'm new to programming and need some help.
I want to use 'if statment' in which it tells the user that the entered value is not found.see below
#include "stadafx"
int main()
{
int paris;
paris = 200;
printf("where do you want to go?\n");
scanf("%d", &paris);
printf("please pay $%d \n.",paris);
return 0;
}
this is a program that i have written and what i want is that if a user enters another city,to tell the user that city not found...please help me.i'm using visual c++ 2010.thanku
Last edited on
Tell us exactly what do you want for your program to do.
Step by step, line by line.

from what i see in your code, i can suppose, that:

1. you create int paris with value 200
2. user changes value of paris by something he enters
3. you try print the value entered by user (if so, the line should look like that:
printf("please pay $%d \n.", paris);)

is that really what you wanted to achieve? What do you want to test using if statement?
if ( statement) { action }
else { action }

It is quite simple.
For exampe:

1
2
3
4
5
6
7
8
9
10
11
12
#include <iostream>
using namespace std;

int main()
{
int x;
cin >> x;

if(x>0) cout << "It is positive";
else cout << "It is non-positive";
return 0;
}


You have variable x. Cin gets input from keyboard and assigns that to variable x. And then we check - if our variable x is greater than 0, we output that it is positive number, else (if "if" statement is not true, we go further and find else. ) doesn't check anything. It just do the code written. But if the "if" statement is true, it doesn't go further to else, it just "jumps" out of the function.
closed account (G8hb7k9E)
Thanks.sorry if my understanding about if statment is wrong..simply i want to print out

printf("sorry the city you entered could not be found\");

if the city that the user entered is not paris.meaning any other word,numbers...etc
so how can i do that?
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>
#include <string>
using namespace std;

int main()
{
const string city_which_is_accepted = "Paris";
string city_which_will_be_entered;
cin >> city_which_will_be_entered;

if(city_which_will_be_entered == city_which_is_accepted)
{
   //write your code, if entered city is correct
}
else
{
  cout << "Sorry, the city you entered could not be found\n";
}
}
Last edited on
or, if you prefer to stay with C and use fragments of code you have written:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <stdio.h>
#include <string.h>

int main()
{
    int paris = 200;
    char name[20];
    printf("where ?\n");
    scanf("%s", &name); /* store user's entered data in string name */
    if(!strcmp("Paris",name))/* strcmp returns 0 if comparision is success */
       printf("please pay %d\n", paris); 
    else printf("Sorry the city you entered could not be found\n");
    return 0;
}
Last edited on
closed account (G8hb7k9E)
Thanks cobalt it worked.and now i want to add another 3 cities.what i have to do?
Also want
printf("sorry the city you entered could not be found,please enter again\n"); i can write it but want to repeat the scanf("%d",paris); until the correct city is entered.thankyou
Topic archived. No new replies allowed.