I wanna make a application like this:
this is console for example:
write_number 5 Your number is 5
How do that?
Can someone explain ?
I wanna make a application like this:
this is console for example:
write_number 5 Your number is 5
How do that?
Can someone explain ?
int num = 0;
printf("write_number ");
cin >> num;
cout << "Your number is " << num;
Use console input and output. These are exposed most simply in C++ by std::cin and std::cout:
http://www.cplusplus.com/doc/tutorial/basic_io/
#include <iostream>
int main(int argc, char* argv[])
{
int value;
std::cout << "write_number ";
std::cin >> value;
std::cout << "Your number is " << value << "\n";
return 0;
}
For how best to use these features, check out this FAQ:
http://www.parashift.com/c++-faq-lite/input-output.html
Edit
If you're trying to get command line arguments to your program, such that your session looks like this:
C:\Users\MyUserName> my_program 5
Your number is 5
Then you use the arguments passed to the main
function. This is an array of all the parameters you passed to the program when you ran it:
#include <iostream>
int main(int argc, char* argv[])
{
std::cout << "Your number is " << argv[1] << "\n";
}
The arguments passed in are in string (textual) form, though. If you want to convert them to a number, so that you can do arithmatic or perform comparisons with them, here is a way to do that:
http://www.parashift.com/c++-faq-lite/misc-technical-issues.html#faq-39.2
#include <iostream>
#include <sstream>
#include <string>
#include <stdexcept>
class BadConversion : public std::runtime_error
{
public:
BadConversion(std::string const& s)
: std::runtime_error(s)
{ }
};
template<typename T>
T ConvertTo(std::string const& s)
{
std::istringstream i(s);
T x;
if (!(i >> x))
throw BadConversion("convertTo(\"" + s + "\")");
return x;
}
int main(int argc, char* argv[])
{
int first_parameter = ConvertTo<int>(argv[1]);
std::cout << "Your number is " << first_parameter << "\n";
return 0;
}
If you're hoping to learn programming by asking questions on Stack Overflow, you're going to be at it for a long time. I would recommend getting Programming -- Principles and Practice Using C++ or Accelerated C++.
As to your question:
#include <iostream>
#include <cstdlib>
int main(int argc, char** argv)
{
std::cout << "your number is " << std::atoi(argv[1]) << '\n';
}
Please note this is not the best version of this program (eg., what if the user doesn't pass an argument, or doesn't pass a number as an argument, or passes a number larger than an int
, or passes a number that is a float
or double
instead of an int
?), but it does give you an idea.
More advanced topics -- without buying the books -- can be found at Bjarne Stroustrup's technical FAQ (Stroustrup created the original versions of C++).
How about we step through this.
First, we want to ask the user for input, how would we go about doing this?
// TODO: Ask user for input.
Once we have that input, how would we go about constructing the new string?
// TODO: Make new string.
Now that we have the new string, how do we display it to the user?
// TODO: Display the string.
This leaves use with the following skeleton for you to fill out:
#include <iostream>
int main()
{
// TODO: Ask user for input.
// TODO: Make new string.
// TODO: Display the string.
return 0;
}
To accomplish this, you could use cout
, cin
, and string
. (Of course you could do the string formatting directly in cout as well)
If you're looking to do something a little more interactive, but still want to work in the console, look into curses.