If you want the user to input an integer value into your program for a variable named number, what arc two lines of code you could write to ask the user to do it and to input the value into your program?
You can use std::cout to output to the console and ask the user for input, and use std::cin to capture input from the user.
If you want to store the value, you will need a third line of code to declare a variable that will be used to store the integer value.
#include <iostream>
usingnamespace std;
int main()
{
int a; // so this will be the number that the user will input.
cout << "Enter any number:";
cin >> a;
//lets say you want to multiply the number that the user inputted by 2.
int b = a *2;
//finally we want to print out the result
cout << b << endl;
system("pause");
return 0;
}
Oops. I didn't know that someone else had replied.
Either way, the code we gave you can do what you want.
I assume this isn't for a class or anything like that.
You could do something like this:
1 2 3 4 5 6 7 8 9 10 11
#include<iostream> //you need to include this to use std::cin and std::cout
int main()
{
int number; //you need a variable, otherwise the program won't know what the user is going to input
std::cout << "Please enter a number" << std::endl;
std::cin >> number;
return 0;
}
std::endl does more than just show a new line, but you don't need to worry about that.
You should also initialize the number to something (usually 0).
You can initialize it by changing int number to int number = 0.
Some compilers will warn you if you don't initialize a variable, and some other compilers will refuse to compile if you don't initialize.
After the user inputs the number, you can do whatever you want to do with the number.
Im actually getting ready for a lab, i don t know what the lab will be about so I m going after book problems to get a grasp. I just started c++ 3 weeks ago. Thank you so much! For helping and taking the time to do it.
If you just started C++, then the lab probably won't be too hard. It will probably cover the stuff you have learned in class over the past couple weeks.
Good job going after book problems - keep doing that and you'll have a better understanding than most of the other students! Believe me, in my labs almost no one prepares before hand - there is plenty of keyboard banging in my labs.
hahahahahaha, i just love this, but i end up looking like an idiot, when what I really needs is somebody with patience to explain with examples... I really do appreciate it....
I saw another interesting one that asks this,
Write a program in C++ that converts from miles to kilometers. Your program should have a reasonable prompt for the user to enter a number of miles and print result.
is the same process for something like this??
We all started as a beginner, and everyone here (as far as I can tell) is more than happy to help with any sort of problem, be it conceptual or just a program not doing what you want (we've all had a program go haywire before).
What most of us won't do is just give you all the code for your program, but we will answer any questions that you have about your program, why it might not be working, and how you can improve it.
Write a program in C++ that converts from miles to kilometers. Your program should have a reasonable prompt for the user to enter a number of miles and print result.
It is a similar process. You will ask the user to input a number of miles, your program will convert it to kilometers, and then it will output the miles as kilometers.
You should use double for this, as miles to kilometers will leave you with decimal points.
Your program would multiply the number of miles by a conversion factor to obtain the number of kilometers, and then output that result. A Google search will send you to plenty of places that can tell you how to convert from miles to kilometers.
#include <iostream>
usingnamespace std;
int main() // start of main function
{
constfloat KM_PER_MILE = 1.609;
float miles,
kms;
// Get the distance in miles.
cin >> miles;
cout << "The distance in miles is " <<
miles << endl;
// Convert the distance to kilometers.
kms = KM_PER_MILE * miles;
// Display the distance in kilometers.
cout << "The distance in kilometers is " <<
kms << endl;
return 0;
}
You can ask Google to convert a number of miles for you to make sure that the conversion is correct.
You don't actually have to use constfloat KM_PER_MILE to do the conversation. You could just do this: kms = 1.609 * miles, but if using KM_PER_MILE makes more sense to you, then there is no reason why you shouldn't use it.
Good commenting. Keep up the habit of commenting your code - it makes it much easier for another program to read your code and see what it is that you are trying to do.
My questions for this one is, how can i call the function i just created but from another program, lets say a program similar but that calls the converter.
Ah, what you want to do is tell the program to launch another program?
That might be a little advanced.
But, when you get further in your course, you will learn about functions. A program can have multiple functions, and you can make a function that will do the conversion for you.
You can think of functions sort of like the math version of functions: You give it input values, and (most of the time) the function will give you back something after it's done doing whatever it is that you want it to do. There are also functions that don't give you back any value, but you will learn about those later.
Both will do the same thing, but the first one is shorter and uses slightly less memory.
All you have to do is call convert_to_kilometers from inside main and pass miles as the argument (the number of miles that the user entered).
You'll use void functions if you want to manipulate something or show some output but you don't need to actually return any value (like showing a menu).
The more you get into C++, the more you'll use functions. They might seem silly now, but they are actually very useful.