Quantcast
Channel: Command Line Argument - Stack Overflow
Viewing all articles
Browse latest Browse all 4

Command Line Argument

$
0
0

I am having trouble figuring out how to make this code work. What is suppose to is depending on the arguments giving from the command line, it suppose to print out a greeting.

int main (int argc, char *argv[]) {double testscore;    if (argc == 2) {        printf("Hello, Mr.%s.\n", argv[1]);    }    else if (argc == 3 && argc == testscore) {        testscore = atof(argv[2]);        printf("Hi, Mr.%s, your score is %.1f\n", argv[1], testscore);    }    else  {        printf("My name is %s %s.\n", argv[1], argv[2]);    }}

If someone puts only their last name, then the terminal will print out...

Hello, Mr. last_name

...because they only put in one argument. This works fine.

The part where I am stuck on is when the command line arguments given are == 3. If 3 arguments are given then either the terminal is suppose to print out...

Hi, Mr. last_name, your test score is test_score

...or...

My name is first_name last_name.

If I put in the command line arguments only the last name and test score (Smith 3.4) then it prints out (example using the last name Smith) then it prints out...

My name is Smith 3.4

However, it does work for putting in the first name and last name (John Smith). This gives...

My name is John Smith.

I do not want the answer, I just want what I am doing wrong and hints on how to fix it.


Viewing all articles
Browse latest Browse all 4

Latest Images

Trending Articles



Latest Images