Jump to content


Friends, we are happy to invite you to our NEW Global Kaspersky Club! Please follow this link www.kasperskyclub.com , sign-in and enjoy our new platform!


Photo
- - - - -

on C language problem


  • Please log in to reply
15 replies to this topic

#1 ruyun

ruyun

    Newbie

  • Members
  • 16 posts

Posted 08 July 2007 - 04:22 PM

Hi,all. I need to help! Who did ever use C language, and konw how to use gets().

When you make some user-define functions and must transfer this function many times. Do you konw how to deal with it? Plaese attention this, maybe when you trasfer some user-define function which includes the same gets funtion once and press the enter and then the enter will change into the sign of end which is used for the string. Maybe the gets function(just have only one) won't work for the function have the sign of end in the character array. So I must do with some way to deal with the gets function.
Is it right?

Thanks in advance for giving me some ideas about gets function!

#2 ruyun

ruyun

    Newbie

  • Members
  • 16 posts

Posted 08 July 2007 - 06:14 PM

Maybe I must add some messages to this problem!
And when we use the gets function in the loop, some unexpected problems will happen!
For example:

#include<stdio.h>
void main()
{
char array[10];
int key;
do
{
gets(array);
puts(array);
printf("\nplease input key!\n");
scanf("%d",key);
}while(key);
}


If we input 1 to key in order to loop, and then we will find we can't input any letters to the array?
How do deal with it to input some numbers to the array,again!

#3 Lucian Bara

Lucian Bara

    Jedi Master

  • Global Moderators
  • PipPipPipPipPip
  • 912 posts

Posted 08 July 2007 - 08:18 PM

well you have two issues.
1) scanf, it requires a pointer, so scanf("%d", &key); to get the correct value
2)the problem is that your gets will register the enter character from when you input the key for scanf at the next step.
a simple solution would be, create a small array, and use gets on it after the scanf to read the enter character. that should work.
your code should look like this

#include<stdio.h>
void main()
{
char array[10], aux[3];
int key;
do
{
gets(array);
puts(array);
printf("\nplease input key!\n");
scanf("%d", &key);
gets(aux);
}while(key);
}
Intel Core 2 Duo E4500 @ 2,2GHz, Msi Asus P5PL2-E, 2048MB-DDR2 (2*1024), Leadtek PX6600 256MB, Teac DV-W516GA, Leadtek tv2000 xp Expert, HDD 200GB*2 (sata/sata2)

#4 ruyun

ruyun

    Newbie

  • Members
  • 16 posts

Posted 11 July 2007 - 05:26 PM

well you have two issues.
1) scanf, it requires a pointer, so scanf("%d", &key); to get the correct value
2)the problem is that your gets will register the enter character from when you input the key for scanf at the next step.
a simple solution would be, create a small array, and use gets on it after the scanf to read the enter character. that should work.
your code should look like this

#include<stdio.h>
void main()
{
char array[10], aux[3];
int key;
do
{
gets(array);
puts(array);
printf("\nplease input key!\n");
scanf("%d", &key);
gets(aux);
}while(key);
}



Hi,Lucian. Thanks for your advice. Your answer reminds of this:When you input some floating-points to array will happen the error of float-link. The wrong code just like this:

void main()
{
int i;
float array[10];
pritnf("Please input the numbers to the array!");
for(i=0;i<9;i++)
scanf("%f",&array[i]);
}

If you add the temp variable to those codes, you'll resovle this problem. The code like this:

void main()
{
int i;
float array[10],temp;
pritnf("Please input the numbers to the array!");
for(i=0;i<9;i++)
{scanf("%f",&temp);array[i]=temp;}
}

But I don't konw why we must do like that just like what you do!

#5 Lucian Bara

Lucian Bara

    Jedi Master

  • Global Moderators
  • PipPipPipPipPip
  • 912 posts

Posted 11 July 2007 - 06:20 PM

{
int i;
float array[10];
pritnf("Please input the numbers to the array!");
for(i=0;i<9;i++)
scanf("%f",&array[i]);
}


that should theoretically work, what's the exact error?
Intel Core 2 Duo E4500 @ 2,2GHz, Msi Asus P5PL2-E, 2048MB-DDR2 (2*1024), Leadtek PX6600 256MB, Teac DV-W516GA, Leadtek tv2000 xp Expert, HDD 200GB*2 (sata/sata2)

#6 ruyun

ruyun

    Newbie

  • Members
  • 16 posts

Posted 11 July 2007 - 07:10 PM

that should theoretically work, what's the exact error?


Sorry, the wrong code is:


void main()
{
int i,j;
float array[3][3];
pritnf("Please input the numbers to the array!");
for(i=0;i<3;i++)
for(j=0;j<3;j++)
scanf("%f",&array[i]);
}


The right code is:


void main()
{
int i,j;
float array[3][3],temp;
pritnf("Please input the numbers to the array!");
for(i=0;i<3;i++)
for(j=0;j<3;j++)
{scanf("%f",&temp);array[i][j]=temp;}
}


I think it doesn't any similar explaition for the errors. But I want to konw why those errors will happen. And please tell the difference between gets() and scanf().


#include<stdio.h>
void main()
{
char array[10];
int key;
do
{
gets(array);
puts(array);
printf("\nplease input key!\n");
scanf("%d",&key);
}while(key);
}



#include<stdio.h>
void main()
{
char array[10];
int key;
do
{
scanf("%s",array);
puts(array);
printf("\nplease input key!\n");
scanf("%d",&key);
}while(key);
}



We'll find the scanf() can run correctly in loop, but the gets() can't. Why? What's the difference between them? I expect your replay, Lucian!

#7 Lucian Bara

Lucian Bara

    Jedi Master

  • Global Moderators
  • PipPipPipPipPip
  • 912 posts

Posted 11 July 2007 - 07:19 PM

of course the first one is wrong, you are using a bidimentional array but when reading it magically turned to a unidimentional one?
scanf("%f",&array[i][j]);

difference, to put it in laymans turn gets() will get a line of text until the \r charcater (13 ASCII), and put it into a string

scanf is more complex, you can format, for example scanf ("bla bla bla %d bla %f ",...) it can just read an integer and a float from the whole string, but reading a whole line of text is impossible with just one command, a string is only read until " "
Intel Core 2 Duo E4500 @ 2,2GHz, Msi Asus P5PL2-E, 2048MB-DDR2 (2*1024), Leadtek PX6600 256MB, Teac DV-W516GA, Leadtek tv2000 xp Expert, HDD 200GB*2 (sata/sata2)

#8 ruyun

ruyun

    Newbie

  • Members
  • 16 posts

Posted 12 July 2007 - 03:53 AM

of course the first one is wrong, you are using a bidimentional array but when reading it magically turned to a unidimentional one?
scanf("%f",&array[i][j]);

difference, to put it in laymans turn gets() will get a line of text until the \r charcater (13 ASCII), and put it into a string

scanf is more complex, you can format, for example scanf ("bla bla bla %d bla %f ",...) it can just read an integer and a float from the whole string, but reading a whole line of text is impossible with just one command, a string is only read until " "



Sorry! I input the code wrongly.


The wrong code is :
void main()
{
int i,j;
float array[3][3];
printf("Please input the numbers to the array!");
for(i=0;i<3;i++)
for(j=0;j<3;j++)
scanf("%f",&array[i][j]);
}



those codes is wrong. The error is scanf: floating points formats not linked Abnormal program termination



I don't konw the error will happen.

#9 Lucian Bara

Lucian Bara

    Jedi Master

  • Global Moderators
  • PipPipPipPipPip
  • 912 posts

Posted 12 July 2007 - 08:22 AM

what are you using, visual, borland, gcc, which version etc.?
Intel Core 2 Duo E4500 @ 2,2GHz, Msi Asus P5PL2-E, 2048MB-DDR2 (2*1024), Leadtek PX6600 256MB, Teac DV-W516GA, Leadtek tv2000 xp Expert, HDD 200GB*2 (sata/sata2)

#10 ruyun

ruyun

    Newbie

  • Members
  • 16 posts

Posted 12 July 2007 - 02:00 PM

what are you using, visual, borland, gcc, which version etc.?


TC 2.0 and Win-TC

TC 2.0 and Win-TC



TC 2.0 is Turbo C 2.0

#11 Lucian Bara

Lucian Bara

    Jedi Master

  • Global Moderators
  • PipPipPipPipPip
  • 912 posts

Posted 12 July 2007 - 02:06 PM

i have no experience with that one, but it might not accept that. my advise would be to move up in the world.
devcpp is free to use http://www.bloodshed.net/devcpp.html
Intel Core 2 Duo E4500 @ 2,2GHz, Msi Asus P5PL2-E, 2048MB-DDR2 (2*1024), Leadtek PX6600 256MB, Teac DV-W516GA, Leadtek tv2000 xp Expert, HDD 200GB*2 (sata/sata2)

#12 ruyun

ruyun

    Newbie

  • Members
  • 16 posts

Posted 12 July 2007 - 05:27 PM

i have no experience with that one, but it might not accept that. my advise would be to move up in the world.
devcpp is free to use http://www.bloodshed.net/devcpp.html



Thank you all the same! And new topic about C language may will come out. And I will go on testing my programe, if the problem still exsit, then I will creat a new topic! And thank you for the software that you recommended.

#13 Lucian Bara

Lucian Bara

    Jedi Master

  • Global Moderators
  • PipPipPipPipPip
  • 912 posts

Posted 12 July 2007 - 05:33 PM

you should know some things first.
1)this one uses gcc so main has to be int and return a value (not that big of a deal)
2)it's recommended to create a project and work on that, also because you might be using more then one file, so it's easier to keep things organzied
Intel Core 2 Duo E4500 @ 2,2GHz, Msi Asus P5PL2-E, 2048MB-DDR2 (2*1024), Leadtek PX6600 256MB, Teac DV-W516GA, Leadtek tv2000 xp Expert, HDD 200GB*2 (sata/sata2)

#14 ruyun

ruyun

    Newbie

  • Members
  • 16 posts

Posted 13 July 2007 - 08:56 AM

you should know some things first.
1)this one uses gcc so main has to be int and return a value (not that big of a deal)
2)it's recommended to create a project and work on that, also because you might be using more then one file, so it's easier to keep things organzied



Thank you for your advice? But what means this sentence: 1)this one uses gcc so main has to be int and return a value (not that big of a deal)


Yes, I do a subject! It's called Personal Finance Manage System. So there will be many problems happen! And thanks a lot for attention this problem. But maybe new topic will be created.

#15 Lucian Bara

Lucian Bara

    Jedi Master

  • Global Moderators
  • PipPipPipPipPip
  • 912 posts

Posted 13 July 2007 - 09:07 AM

you use void main. now you have to use int main, and the function must return a value, it's good too, because you can return an error code when it's faulty. Example

int main()
{
FILE *fData;
if(fData=fopen("file.txt", "r")==NULL)
return 2; //the application exits returning the vaule 2, which you can use if this is a component of a larger program
else
......
return 1; //it returns 1
}
Intel Core 2 Duo E4500 @ 2,2GHz, Msi Asus P5PL2-E, 2048MB-DDR2 (2*1024), Leadtek PX6600 256MB, Teac DV-W516GA, Leadtek tv2000 xp Expert, HDD 200GB*2 (sata/sata2)