LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 06-24-2002, 08:31 AM   #1
narius
LQ Newbie
 
Registered: Jun 2002
Location: Sweden
Distribution: Slack
Posts: 18

Rep: Reputation: 0
simpel person program in c


Tja

I learning me c i use the book programing in ansi c.
but i´m not sure about scanf so i want to do a simpel program thats ask: name, age, hometown, street at hometown

i know i shold use tex printf("write you name\n");
then the scanf statement
 
Old 06-24-2002, 11:09 AM   #2
crabboy
Senior Member
 
Registered: Feb 2001
Location: Atlanta, GA
Distribution: Slackware
Posts: 1,821

Rep: Reputation: 121Reputation: 121
For educational proposes only. Never use code like this in non useless program.

Code:
#include <stdio.h>

struct Info
{
   char szName[50];
   int  iAge;
   char szAddress[50];
   char szCity[50];
}; 

main()
{
   struct Info stInfo;

   printf( "Gimmie your name: " );
   scanf( "%s",  stInfo.szName );

   printf( "Gimmie your age: " );
   scanf( "%d",  &stInfo.iAge );

   printf( "Gimmie your address: " );
   scanf( "%s", stInfo.szAddress );

   printf( "Gimmie your City: " );
   scanf( "%s", stInfo.szCity );

   printf( "Name: [%s], Age: [%d], Address: [%s], City: [%s]\n",
           stInfo.szName,
           stInfo.iAge,
           stInfo.szAddress,
           stInfo.szCity ); 

}
 
Old 06-24-2002, 11:21 AM   #3
narius
LQ Newbie
 
Registered: Jun 2002
Location: Sweden
Distribution: Slack
Posts: 18

Original Poster
Rep: Reputation: 0
pleas say what the different part are doing
 
Old 06-24-2002, 11:25 AM   #4
narius
LQ Newbie
 
Registered: Jun 2002
Location: Sweden
Distribution: Slack
Posts: 18

Original Poster
Rep: Reputation: 0
i tack the last message back.
but i have to ask.
why do i have only have "s" att the char variabel
and why char Name[50]; do i have 50 there

as i say befor i want to learn and you can´t learn if you doisent ask

Last edited by narius; 06-24-2002 at 11:26 AM.
 
Old 06-24-2002, 12:52 PM   #5
crabboy
Senior Member
 
Registered: Feb 2001
Location: Atlanta, GA
Distribution: Slackware
Posts: 1,821

Rep: Reputation: 121Reputation: 121
The "%s" arguement in scanf tells it to convert what it finds in the buffer to a string. The "%d" tells it to convert it to a decimal (int). the szName[50] creates an array of 50 characters. This will let you copy a string of up to 50 characters to it.
 
Old 06-24-2002, 01:26 PM   #6
narius
LQ Newbie
 
Registered: Jun 2002
Location: Sweden
Distribution: Slack
Posts: 18

Original Poster
Rep: Reputation: 0
thanx
 
Old 06-26-2002, 01:06 PM   #7
Robert0380
LQ Guru
 
Registered: Apr 2002
Location: Atlanta
Distribution: Gentoo
Posts: 1,280

Rep: Reputation: 47
just as a security tip, i've been told that scanf is not safe and can cause buffer over flows (often used by hackers to root machines) and also never ever use gets()
 
Old 06-27-2002, 02:40 PM   #8
Eits0
Member
 
Registered: May 2002
Location: Finland
Distribution: Mandrake 9.0
Posts: 188

Rep: Reputation: 30
Ooh! C++ seems simplier in this part!

Code:
printf( "Gimmie your name: " );
   scanf( "%s",  stInfo.szName );
instead of scanf, just use cin >> !!!
/Meh likes c++!!!
 
Old 10-11-2003, 07:41 AM   #9
pmedes
LQ Newbie
 
Registered: Aug 2003
Location: Owatonna MN
Distribution: SuSE pro 9.1
Posts: 16

Rep: Reputation: 0
Angry 500 error message

I tried using the spider program in Sean Burke's "Perl and LWP" book
and, no matter what URL I start out with, I get a 500 message "URIs have
been disabled". Is this some sort of security issue?
Thanks,
Phil
 
Old 10-11-2003, 07:54 AM   #10
pmedes
LQ Newbie
 
Registered: Aug 2003
Location: Owatonna MN
Distribution: SuSE pro 9.1
Posts: 16

Rep: Reputation: 0
I found the 500 problem; it was just a typo.
Sorry!
 
Old 10-16-2003, 03:17 PM   #11
Robert0380
LQ Guru
 
Registered: Apr 2002
Location: Atlanta
Distribution: Gentoo
Posts: 1,280

Rep: Reputation: 47
never use scanf, it's an insecure method (buffer overflow)

use fgets or cin, never ever ever ever use scanf if you care about the security of your programs.
 
Old 10-16-2003, 11:34 PM   #12
darkseed2g3
Member
 
Registered: Sep 2003
Location: Philadelphia ,Pa
Distribution: Fedora Core 1 BABY !!! YEA
Posts: 67

Rep: Reputation: 19
cin can be buffer overrun to caues it doesnt check for out of bounds unless done properly but most programmers arent very "proper" bout there code. I wish when ppl learned c/c++ or the jerks who wrote the books would teach more practical ways of programming.
 
  


Reply



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

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Person who wants to try Linux SomeNewbie LinuxQuestions.org Member Intro 1 06-03-2005 09:08 PM
Can you PM more than one person at a time? Whitehat LQ Suggestions & Feedback 2 01-02-2004 05:19 PM
Making a simpel backup CloudBuilder Linux - Newbie 4 10-30-2003 12:13 PM
Help making simpel Squid.confd Gadget Linux - Networking 3 10-23-2003 10:14 AM
Probably a very simpel samba problem Jonathan Archer Linux - Networking 18 03-20-2003 04:44 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 01:29 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration