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
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?
#include <iostream>
usingnamespace 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.
#include <iostream>
#include <string>
usingnamespace 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";
}
}
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;
}
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