遊客:  註冊 | 登錄 | 幫助





標題: [求助] help me in programming.....
  onlylonely     Rank: 2Rank: 2
藍之初
性別 男
UID 13870

精華 0
帖子 70
積分 100   詳情

閱讀權限 30
註冊 2006-10-13
來自
狀態 離線

 
 
 
 
發表於 2007-3-11 03:36 PM  資料  個人空間  短訊  加為好友 
help me in programming.....

i want to type "handsome guy" but i when i type jux get "handsome" only........

int main()
{
    char name[30];
   
    printf("Enter your name: ");
    scanf("%s",name);
   
    printf("%your name is %s",name);
    printf("\n");

return 0;
}

頂部

  onlylonely     Rank: 2Rank: 2
藍之初
性別 男
UID 13870

精華 0
帖子 70
積分 100   詳情

閱讀權限 30
註冊 2006-10-13
來自
狀態 離線

 
 
 
 
發表於 2007-3-11 04:29 PM  資料  個人空間  短訊  加為好友 
i found the ways liao.......

printf("Enter your name:");
    gets(name);

  printf("%your name is %s",name);
    printf("\n");
   

haha...........then one more question...........how to use "fgets"???

fgets(name,30, ????)...............??? put what????

頂部



  onlylonely     Rank: 2Rank: 2
藍之初
性別 男
UID 13870

精華 0
帖子 70
積分 100   詳情

閱讀權限 30
註冊 2006-10-13
來自
狀態 離線

 
 
 
 
發表於 2007-3-11 05:09 PM  資料  個人空間  短訊  加為好友 
emmmmmmmmm............one more question................how i can write my name in this

*
**
***
****
*****
******     My name
*******
********

how to do it????

頂部

mickeyGoUp     Rank: 7Rank: 7Rank: 7
版主
性別 男
UID 5

精華 0
帖子 35511
積分 5235   詳情

閱讀權限 150
註冊 2006-3-24
來自 美國滴滴尼
狀態 離線

 
 
 
 
發表於 2007-3-11 11:22 PM  資料  個人空間  短訊  加為好友 
syntax for fgets is:  fgets ( str, num, f );

f is a handle to the file.  For example:

f = fopen ("myFile.txt" , "r");

Of course, you should add some checking after the fopen to make sure it opened correctly before accessing the file.  E.g.,

if (f== NULL) perror ("Error opening file");

Hope that helps.  

頂部

mickeyGoUp     Rank: 7Rank: 7Rank: 7
版主
性別 男
UID 5

精華 0
帖子 35511
積分 5235   詳情

閱讀權限 150
註冊 2006-3-24
來自 美國滴滴尼
狀態 離線

 
 
 
 
發表於 2007-3-11 11:28 PM  資料  個人空間  短訊  加為好友 


QUOTE:
原帖由 onlylonely 於 2007-3-11 04:09 AM 發表
emmmmmmmmm............one more question................how i can write my name in this

*
**
***
****
*****
******     My name
*******
********

how to do it????

You need to use two loops.  I forgot the exact syntax but you can figure out that part yourself I believe.  The idea is something like:

for (i=1; i<=8; i++) {
    for (j=1; j<=i; j++) {
        printf("*");
    }
    if (i == 6) {
        printf(" %s", name);
    }
    printf("\n");
}

The first loop count the line, and the inner loop count the number of star u wanna print out in each line.  Since the number of stars is equal to the line number, so in the inner loop u may just use "i" (the line number) as the number of stars.

頂部

  onlylonely     Rank: 2Rank: 2
藍之初
性別 男
UID 13870

精華 0
帖子 70
積分 100   詳情

閱讀權限 30
註冊 2006-10-13
來自
狀態 離線

 
 
 
 
發表於 2007-3-12 11:34 AM  資料  個人空間  短訊  加為好友 
emmmmmmmmm....i still don understand the fgets wo..........

頂部

mickeyGoUp     Rank: 7Rank: 7Rank: 7
版主
性別 男
UID 5

精華 0
帖子 35511
積分 5235   詳情

閱讀權限 150
註冊 2006-3-24
來自 美國滴滴尼
狀態 離線

 
 
 
 
發表於 2007-3-12 11:51 AM  資料  個人空間  短訊  加為好友 
When you use fgets(), you are reading from a file, right?  So the third parameter is to tell fgets() where to get the input.  Just like gets() is getting the input from the standard input (you type the input manually), fgets() will get the string or input from a file.

So let's say you have a text file which has the content:

QUOTE:
Peter
John
Mary

And you want to use fgets() to read the string "Peter", "John", and "Mary" from the file.  So first of all you open the file:

QUOTE:
FILE * f;
f = fopen ("myFile.txt" , "r");

Now you are ready to call fgets():

QUOTE:
char name[100];
fgets ( name, 100, f );

Then 'name' will store the string "Peter".  It stops at the newline after "Peter".  So it will not read "John" and "Mary" unless you use a loop to make it run multiple times.  Or of course you can do it manually:

QUOTE:
char name[100];
char name2[100];
char name3[100];
fgets ( name, 100, f );
fgets ( name2, 100, f );
fgets ( name3, 100, f );

It works but it is not the best approach.

頂部

  onlylonely     Rank: 2Rank: 2
藍之初
性別 男
UID 13870

精華 0
帖子 70
積分 100   詳情

閱讀權限 30
註冊 2006-10-13
來自
狀態 離線

 
 
 
 
發表於 2007-3-12 08:10 PM  資料  個人空間  短訊  加為好友 
but just now i c a solution is using...........

char name[100]
printf("Entetr your names :")
fgets(name,100,stdin);

noneed open file d wo........
what's mean by stdin ????
standard input ????

頂部

mickeyGoUp     Rank: 7Rank: 7Rank: 7
版主
性別 男
UID 5

精華 0
帖子 35511
積分 5235   詳情

閱讀權限 150
註冊 2006-3-24
來自 美國滴滴尼
狀態 離線

 
 
 
 
發表於 2007-3-12 09:44 PM  資料  個人空間  短訊  加為好友 


QUOTE:
原帖由 onlylonely 於 2007-3-12 07:10 AM 發表
but just now i c a solution is using...........

char name[100]
printf("Entetr your names :")
fgets(name,100,stdin);

noneed open file d wo........
what's mean by stdin ????
standard input ????

You got it!    stdin stands for "standard input".

You can also look at the third parameter as a "file input stream".  And stdin is one type of file input stream that usually associates with the keyboard by default.

If you are reading from the keyboard, gets() is more convinient because you do not have to speicify where the input is coming from.  However, gets() does not check the size of the buffer.  Although it stops reading when it hits a newline or EOF, an overflow on the stack can occur.

Hope that helps.  

頂部

  onlylonely     Rank: 2Rank: 2
藍之初
性別 男
UID 13870

精華 0
帖子 70
積分 100   詳情

閱讀權限 30
註冊 2006-10-13
來自
狀態 離線

 
 
 
 
發表於 2007-3-14 02:47 PM  資料  個人空間  短訊  加為好友 
another question is ,how to do
*********9*********
********8********
   *******7*******
    ******6******
      *****5*****
        ****4****
           ***3***
             **2**
               *1*

頂部

快速美言
           


當前時區 GMT+8, 現在時間是 2024-11-23 03:28 PM

    Powered by Discuz!  © 2001-2007 Comsenz Inc.   
Processed in 0.331924 second(s), 9 queries

清除 Cookies - 聯繫我們 - LIPS Corner 新天藍 - Archiver