tags:

views:

266

answers:

7

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 ?

+6  A: 

Here's a basic tutorial on cin and cout ("see-in/out")

http://www.cplusplus.com/doc/tutorial/basic_io/

Hector204
A: 
int num = 0;
printf("write_number ");
cin >> num;
cout << "Your number is " << num;
maxwellb
printf and cout in the same code'?
JBRWilkinson
That may have problems. `cin` and `cout` are synchronized, but I don't think they synchronize with `printf`. The "write_number" could appear before or after the read from `cin` and/or the write from `cout`.
David Thornley
+4  A: 

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;
}
Merlyn Morgan-Graham
If you are interacting with the user you should force the output buffer to flush before trying to get input. This is easily done with std::endl
Martin York
@Martin: TY. Fixed, I think :)
Merlyn Morgan-Graham
`cin` and `cout` are tied by default (flushing happens automatically where you need it): http://www.cplusplus.com/reference/iostream/ios/tie/ .
Max Lybbert
@Martin, Max: Didn't know that Max. Will revert it back
Merlyn Morgan-Graham
@Merlyn: it's a good detail to know (both that things need to be flushed and that `cin` and `cout` do the flushing automatically). It does come up once in a while ( http://blog.think-async.com/2007/01/unbuffered-socket-iostreams.html ).
Max Lybbert
A: 
int number = 5;
cout << "Your number" << number;
RoR
+9  A: 

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++).

Max Lybbert
+1. Oh, he actually meant to retrieve values via parameters, not console input. Perceptive!
Merlyn Morgan-Graham
The original formatting didn't make that very obvious.
Max Lybbert
+1 for actually answering the question asked.
John Dibling
+2  A: 

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)

Paul
A: 

If you're looking to do something a little more interactive, but still want to work in the console, look into curses.

dj_segfault