Create a program that will perform the following task: Write an if statement tha
ID: 3849625 • Letter: C
Question
Create a program that will perform the following task: Write an if statement that will print out your full name (first and last) IF the user types in the number 1.
This is what I have and it's not working right. If you enter any number is prints out my name with a one at the end of it.
#include <iostream>
using namespace std;
int main()
{
int number;
cout<<"Please enter a number between 1 and 10. The goal is to figure out which number will make my name appear on the screen: ";
cin>>number;
if (number = 1)
{
cout<<endl<<"Sandra Bullock"<<number<<endl;
if (number!=1)
{
cout<<"The number you entered is not correct"<<endl;
}
}
system("pause");
return 0;
}
Explanation / Answer
#include <iostream>
#include <cstdlib>
using namespace std;
int main()
{
int number;
cout<<"Please enter a number between 1 and 10. The goal is to figure out which number will make my name appear on the screen: ";
cin>>number;
if (number == 1)// = is for assignment and == is for comparison
{
cout<<endl<<"Sandra Bullock"<<number<<endl;
}
else
{
cout<<"The number you entered is not correct"<<endl;
}
system("pause");
return 0;
}
Output:
Please enter a number between 1 and 10. The goal is to figure out which number will make my name appear on the screen: 1
Sandra Bullock1
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.