Help using argc/argv in assignment

Use and complete the template provided. The entire template must be completed. If you don't, your post may be deleted!

  1. The problem statement, all variables and given/known data:
    First, create a "hello world" program that prints "Hello World". But NOW, instead use argc to verify that a parameter was given, use argv[1] to display the command line argument back to the user after displaying "Hello". For instance, if I type

./helloWorld Michael
I will see
-----> Hello Michael

  1. Relevant commands, code, scripts, algorithms:
    We are first asked to create a "hello world" program that simply prints "Hello World"
    I typed what was given to me from the assignment:
#include <stdio.h>

int main (int argc, char *argv[])
{
        char *message = "Hello World!";
        printf("%s\n, message);
        return 0;
}
  1. The attempts at a solution (include all code and scripts):
#include <stdio.h>

int main (int argc, char *argv[])
{
        char *message = "Hello World!";
        printf("%s\n", message);
        
if(argc >= 2, argv)
{
       printf("%s_%s", argv[1])
}
return 0;
}
  1. Complete Name of School (University), City (State), Country, Name of Professor, and Course Number (Link to Course):
    Western Illinois University(WIU), Macomb(Illinois), USA, Justin EHRLICH, CS410(https://westernonline.wiu.edu/d2l/home/58052\)

Note: Without school/professor/course information, you will be banned if you post here! You must complete the entire template (not just parts of it).

I'm pretty stuck. It's not giving me the right output and I don't have any experience using C++. Been trying to modify it all day and can't get any help. I'd appreciate any help at all. :slight_smile:

#include <stdio.h>

int main (int argc, char *argv[])
{
        char *message = "Hello ";
        printf("%s ", message);

if(argc == 2)
{
       printf("%s!\n", argv[1]);
}
else
{
        printf ("world!\n");
}

return 0;

}
1 Like

Another way:

int main(int argc, char *argv[])
{
        char *message="Hello";
        char *who="World!";
        if(argc == 2) 
        {
                who=argv[1];
        }

        printf("%s %s\n", message, who);
        return(0);
}
1 Like

Thank you my friend. This works. The only thing that did not work is the part I bolded in the quote. It keeps printing "n" at the end of the statement instead of doing a new line(which is what I think you're trying to do here?). It's not a big deal because I can cut out that part and get the right output anyway.

---------- Post updated at 01:12 PM ---------- Previous update was at 01:10 PM ----------

Thanks for the alternate solution. Gives me something to think about as well.

That's exactly what "\n" does inside a double-quoted string, yes.

Did you alter it in any way or double up the backslashes, etc? It really ought to work as-is.

It works when I just use the command as "./helloWorld" ...but when I use "./helloWorld John" , it outputs: "Hello John!n" and yea, I definitely have it exactly as you put it. Didn't alter it.

What's your system?

We use uxb3 which is Unix, but then we log in to "toolman" which I think is Linux. I created the program using the Vim editor.

So you sit down at ???, which runs uxb3 -- which sounds more like a hostname than an OS to me -- and open a ??? terminal to run ??? to login to "toolman" which runs ???. Is it possible to run uname -a on any of these systems? Is it possible to fill in any of those question marks?

Yea, uxb3 is the hostname(uxb3.wiu.edu). I know it is Unix. I can access the school's Unix server from anywhere using any computer with the program Putty <PuTTY Download Page> .... which I then log in to "toolman"(which is also the hostname) using command "ssh toolman.wiu.edu" and can log in with my username/password for the school's system. I do not know if

 uname -a 

works on any of the systems. I can get back to you with some betters answers about the actual systems by Monday when I can ask someone at the university's IT dept.

---------- Post updated at 03:00 PM ---------- Previous update was at 02:26 PM ----------

Nevermind, I made a typo here:

In which, I forgot to type '\' after the exclamation point. Problem solved.

1 Like