Click here to Skip to main content
13,048,406 members (146,356 online)
Rate this:
Please Sign up or sign in to vote.
See more:
I want to know how i'm going to wrote a simple C++/C application that accept value in the first place.

For Application name will be "myCommand" and my value is "50". I will wrote in the command line something like this in unix base "myCommand 50".
then my code will receive the value "50" from the command line then it will print back the value

the original code :

#include <stdio.h>
main() {
int count;
puts("Please enter a number: ");
scanf("%d", &count);
printf("The number is %d \n", count);

Please help ??
Posted 25-Mar-13 18:45pm
Updated 25-Mar-13 19:13pm

1 solution

Rate this: bad
Please Sign up or sign in to vote.

Solution 1

Ummm ... Is this a trick question?

#include <iostream>
using namespace std;
int main( int argc, char* argv[] )
  cout << "Program name: " << argv[0]
       << "\nArgument: " << argv[1]; << "\n";
  return 0;

It sounds like you probably want to call ShellExecute() or a related API with argv[1].

[Edit]You editted your question so that it now asks something different; however I think my answer still answers the updated question. Just ignore the "ShellExecute() part.[/Edit]
Right, a 5.
H.Brydon 26-Mar-13 1:25am
Thank you SA. What goes around comes around!
bunge-bunge 26-Mar-13 1:26am
Thank H.Brydon.
You have point me out the way.
I have changed my scenario in my question for you to understand more about what i really mean
Stefan_Lang 28-Mar-13 5:27am
H.Brydon is correct. Once you've started the program, the input stream will jump to the next line. So you can no longer read the additional information from the command line through standard input functions. The _only_ way to retrieve additional arguments from the command line is by accessing argv and argc!

Of course, if your intention is a dialog-based input, as the code you posted suggests, then it doesn't make sense to write "myCommand 50", because that skips over the programs request to enter a number! In that case you'd start your program with just "myCommand", which in turn will print "Please entere a number", and only then you'll type "50".

If your intention is to just pass the value to your program dirctly, use H.Brydons code. If you wish to interact with the user by querying the value, then use your own code, and start your application just with "myCommand".

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Mobile
Web02 | 2.8.170713.1 | Last Updated 26 Mar 2013
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100