GIDForums  

Go Back   GIDForums > Computer Programming Forums > C Programming Language
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 20-Feb-2004, 05:06
andre andre is offline
New Member
 
Join Date: Feb 2004
Posts: 3
andre is on a distinguished road
Question

new to C, need an explanation


I'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:
#include <stdio.h>

char name[10];
char name2[10];

char getname()
{
  printf("Enter your first name : ");
  scanf("%s", &name[10]);
  return name[10];
}

char getname2()
{
  printf("Enter your surname : ");
  scanf("%s", &name2[10]);
  return name2[10];
}

void doname()
{
  printf("Hello %s %s, nice to meet you.\n", name, name2);
}

int main()
{
  doname(getname(),getname2());
  return 0;
}
Last edited by JdS : 20-Feb-2004 at 08:56. Reason: please use the [c]your c code[/c] syntax highlighter
  #2  
Old 20-Feb-2004, 09:25
dsmith's Avatar
dsmith dsmith is offline
Senior Member
 
Join Date: Jan 2004
Location: Utah, USA
Posts: 1,351
dsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of light
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:
scanf("%s", name)
Also, your use of globals and functions is a little confusing. There are two options that I can see here to fix this.

#1: Just use globals:
CPP / C++ / C Code:
#include <stdio.h>

char name[10];
char name2[10];

void getname()
{
  printf("Enter your first name : ");  //No need to return anything as it is global
  scanf("%s", name)
}

void getname2()   //no need to return anything as it is global
{
  printf("Enter your surname : ");
  scanf("%s", name2);
}

void doname()    //There are no parameters called here
{
  printf("Hello %s %s, nice to meet you.\n", name, name2);
}

int main()
{
  getname();
  getname2();
  doname();
  return 0;
}

#2: Use functions (no globals)
CPP / C++ / C Code:

#include <stdio.h>

char name[10];
char name2[10];

char* getname()
{
  char name[10]
  printf("Enter your first name : ");
  scanf("%s", name);
  return name;
}

char* getname2()
{
  char name2[10]
  printf("Enter your surname : ");
  scanf("%s", name2);
  return name2;
}

void doname(char* name, char* name2)
{
  printf("Hello %s %s, nice to meet you.\n", name, name2);
}

int main()
{
  doname(getname(),getname2());
  return 0;
}

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 15:27.
  #3  
Old 20-Feb-2004, 15:13
WaltP's Avatar
WaltP WaltP is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Midwest US
Posts: 3,281
WaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to all
Quote:
Originally Posted by dsmith
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:
scanf("%s", name)
Quote:
Originally Posted by dsmith
Also, your use of globals and functions is a little confusing. There are two options that I can see here to fix this.

#1: Just use globals:

#2: Use functions (no globals)

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:
Originally Posted by dsmith
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.
This is one reason why using scanf() is frowned upon. Use fgets() instead. Another reason specifically in your case is scanf() has a lot of overhead "scanning" and analyzing your input, deciding how to read the input. fgets() reads strings. You are inputting strings. So don't use a 20 pound hammer to open the walnut.

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  
Old 25-Feb-2004, 10:20
andre andre is offline
New Member
 
Join Date: Feb 2004
Posts: 3
andre is on a distinguished road
Question

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  
Old 25-Feb-2004, 12:28
aaroncohn's Avatar
aaroncohn aaroncohn is offline
Regular Member
 
Join Date: Feb 2004
Location: Bay Area, CA.
Posts: 564
aaroncohn is a jewel in the roughaaroncohn is a jewel in the roughaaroncohn is a jewel in the rough
CPP / C++ / C Code:
#include <stdio.h>

char name[10];
char name2[10];

char* getname()
{
  printf("Enter your first name : "); //removed redeclaration of global variable
  scanf("%s", name);
  return name;
}

char* getname2()
{
  printf("Enter your surname : "); //removed redeclaration of global variable
  scanf("%s", name2);
  return name2;
}

void doname(char* name, char* name2)
{
  printf("Hello %s %s, nice to meet you.\n", name, name2);
}

int main()
{
  doname(getname(),getname2());
  return 0;
}

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:
#include <stdio.h>

void getname(char name[]) //arrays are always passed by reference, so you don't need the '&'
{
  printf("Enter your first name : ");
  scanf("%s", name);
}

void getname2(char name2[]) //no need to return anything in either getname function
{
  printf("Enter your surname : ");
  scanf("%s", name2);
}

void doname(char* name, char* name2)
{
  printf("Hello %s %s, nice to meet you.\n", name, name2);
}

int main()
{
  char name[10];
  char name2[10];

  getname2(name2);
  getname(name);
  doname(name,name2);
  return 0;
}

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:
void getname(char* name) //no difference except it's char* instead of char name[]
{
  printf("Enter your first name : ");
  scanf("%s", name);
}

void getname2(char* name2) //same here
{
  printf("Enter your surname : ");
  scanf("%s", name2);
}
Last edited by aaroncohn : 25-Feb-2004 at 13:10.
  #6  
Old 25-Feb-2004, 12:57
aaroncohn's Avatar
aaroncohn aaroncohn is offline
Regular Member
 
Join Date: Feb 2004
Location: Bay Area, CA.
Posts: 564
aaroncohn is a jewel in the roughaaroncohn is a jewel in the roughaaroncohn is a jewel in the rough
Quote:
Originally Posted by WaltP
Use fgets() instead.
Isn't fgets() strictly for file I/O?
  #7  
Old 25-Feb-2004, 14:06
WaltP's Avatar
WaltP WaltP is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Midwest US
Posts: 3,281
WaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to all
Quote:
Originally Posted by aaroncohn
Isn't fgets() strictly for file I/O?
No.
CPP / C++ / C Code:
fgets(buf, num, stdin);
will read from the keyboard. stdin is the standard input device which also means you can pipe a program's output into your program and your program will read said output from stdin. Kinda like the more or sort filters in Windows.

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:
sort <file
The "<" is the input pipe character that will take the file specified and send it into the program to be read from stdin. For completeness, the ">" is the output pipe character for output to stdout.


Maybe I'll set up a tutorial about standard input/output.
  #8  
Old 25-Feb-2004, 14:58
aaroncohn's Avatar
aaroncohn aaroncohn is offline
Regular Member
 
Join Date: Feb 2004
Location: Bay Area, CA.
Posts: 564
aaroncohn is a jewel in the roughaaroncohn is a jewel in the roughaaroncohn is a jewel in the rough
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
  • Preface. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . i
  • Introduction. . . . . . . . . . . . . . . . . . . . . . . . . . . . Chapter 1
  • Getting Started. . . . . . . . . . . . . . . . . . . . . . . . . .Chapter 2
  • Console I/O. . . . . . . . . . . . . . . . . . . . . . . . . . . . Chapter 3
  • Console Stream I/O. . . . . . . . . . . . . . . . . . . . . . . Chapter 4
  • System I/O. . . . . . . . . . . . . . . . . . . . . . . . . . . . .Chapter 5
  • Standard I/O. . . . . . . . . . . . . . . . . . . . . . . . . . . .Chapter 6
  • File Stream I/O. . . . . . . . . . . . . . . . . . . . . . . . . . Chapter 7
  • Additional Examples & Case Studies. . . . . . . . . . . . .Appendix A
  • Bibliography. . . . . . . . . . . . . . . . . . . . . . . . . . . . Appendix B
  • PowerPoint Presentation Slides. . . . . . . . . . . . . . . .Appendix C
Last edited by aaroncohn : 25-Feb-2004 at 15:49.
  #9  
Old 25-Feb-2004, 20:34
WaltP's Avatar
WaltP WaltP is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Midwest US
Posts: 3,281
WaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to all
Kool! I'd like to see it.
  #10  
Old 26-Feb-2004, 00:33
aaroncohn's Avatar
aaroncohn aaroncohn is offline
Regular Member
 
Join Date: Feb 2004
Location: Bay Area, CA.
Posts: 564
aaroncohn is a jewel in the roughaaroncohn is a jewel in the roughaaroncohn is a jewel in the rough
If I can get my hands on a scanner, I can make a pdf for you... I think
 
 

Recent GIDBlogI?m Home by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
preg_replace + regular expression EasyExpat MySQL / PHP Forum 4 10-Jul-2003 05:36

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 06:31.


vBulletin, Copyright © 2000 - 2009, Jelsoft Enterprises Ltd.