![]() |
|
#1
|
|||
|
|||
new to C, need an explanationI'm trying to complete a simple exercise to start off. The following is supposed to allow the user to input their first name and surname and then give them a greeting. I have a couple of questions. Firstly, when I execute the program it asks for my first name, then my second name, but only shows the surname in the greeting. Why?
Secondly, the order of the parameters for the doname function in main() seems to be inversed - ie getname2() is done before getname(), meaning it prompts for the surname before the first name. Could someone please explain what I'm missing? Thanks! CPP / C++ / C Code:
Last edited by JdS : 20-Feb-2004 at 07:56.
Reason: please use the [c]your c code[/c] syntax highlighter
|
|||
|
#2
|
||||
|
||||
|
Hi andre. I think your problem is where you pass your strings to scanf.
You are passing &name[10], which is actually, the last position in that string. what you should pass is just name. CPP / C++ / C Code:
#1: Just use globals: CPP / C++ / C Code:
#2: Use functions (no globals) CPP / C++ / C Code:
Be careful with C, because it does not check for the right size memory. So if your "user" enters a name that is longer than ten charectars, C will still force it into your array and it will overwrite. HTH d Last edited by dsmith : 20-Feb-2004 at 14:27.
|
|
#3
|
||||
|
||||
|
Quote:
Quote:
Just using globals is not generally recommended. The general thought is to make all your variables local and pass them as in #2. This should be tempered IMHO with common sense. Quote:
Also scanf() has many other problems you will run into to make you go bald in frustration. It can't handle keyboard input all that well to be honest. |
|
#4
|
|||
|
|||
|
hey thanks for the help. i got one of your suggestions to work - using global variables. however i couldn't properly compile the second option (using functions with local varialbes). i get:
warning: getname - function returns address of local variable warning: getname2 - " it runs fine until having to print the two strings to screen...i noticed there were a couple of semi-colons missing so i added them, but also noticed that you left the two variables name[10] and name2[10] declared globally at the beginning and then again in each function. removing this doesn't seem to make much difference. where does the problem with the locally-declared variables come from then? sorry if this is all very basic and stupid, i'm just trying to grasp some aspects which haven't been explained in class yet... thanks again |
|
#5
|
||||
|
||||
|
CPP / C++ / C Code:
All you need to do to get dsmith's example to run is to remove the local function declarations of name and name2. Using global variables is tricky, and you should only do it when you absolutely have to. Global variables usually cause side effects that are difficult to program, debug, and use. About the only place where you should use global variables is where you assign values that are needed in many functions but won’t be changed by most functions. In this case, it would be better not to use globals at all. CPP / C++ / C Code:
As I explained in the code directly above, the functions getname() and getname2() do not need to return anything because arrays are always passed by reference. You could use pass the array using char* instead, as well. CPP / C++ / C Code:
Last edited by aaroncohn : 25-Feb-2004 at 12:10.
|
|
#6
|
||||
|
||||
|
Quote:
|
|
#7
|
||||
|
||||
|
Quote:
CPP / C++ / C Code:
For example, find the command line and run sort. It will wait for input. Type a few lines of text then press CTRL-Z & ENTER. sort has read your input from stdin and displayed the sorted info on stdout. Next test is run sort with the following command: Quote:
Maybe I'll set up a tutorial about standard input/output. |
|
#8
|
||||
|
||||
|
One about piping too. I didn't know you could do piping in windows. I only knew of unix piping. I actually have a great reference for I/O in C/C++ written by my instructor, Professor Keith Jolly. Here's a table of contents just to give you an idea of what it covers...
I/O in C/C++ Console and File I/O for DOS< UNIX, and Windows
Last edited by aaroncohn : 25-Feb-2004 at 14:49.
|
|
#9
|
||||
|
||||
|
Kool! I'd like to see it.
|
|
#10
|
||||
|
||||
|
If I can get my hands on a scanner, I can make a pdf for you... I think
![]() |
Recent GIDBlog
Not selected for officer school by crystalattice
| Thread Tools | Search this Thread |
| Rate This Thread | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| preg_replace + regular expression | EasyExpat | MySQL / PHP Forum | 4 | 10-Jul-2003 04:36 |
Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The