Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

NEWBIE IN DESTRESS 1

Status
Not open for further replies.

VspecGTR3

Technical User
Feb 3, 2003
62
0
0
US
im like the subject im new to c im currently reading C for MeatHeads (eheh ok dumbies) and i followed the book and im using mircle c to write and compile but my first script (not counting hello world) wont work i followed the book and it jsut dont seem to get it why it doesnt work take a gander

#include <stdio.h>

void main()
{
char me[20];

printf(&quot;What is your name?&quot;);
scanf(&quot;%s&quot;,&me);
printf(&quot;Darn glad to meet you, %s!\n&quot;,me);
}
 
Well.. first thing is you did not specify what the problem is. CZ the code looks good and will work. What might have happened is you did not use the appropriate compiler options etc.

Be more precise of your problem with all your compiler switches etc.

Cheers
KK IBM MQSeries Specialist
 
im sry im not sure whtat the problem is furst off its not compiling and the code should work but it isnt (whats new?) here the error i get:

Miracle C Compiler (r3.2), written by bts.
Compiling C:\Program Files\Miracle C\c programs\test2.c
main

C:\Program Files\Miracle C\c programs\test2.c: line 8: & non lvalue
'scanf(&quot;%s&quot;,&me)'
aborting compile

im not a smart man but it seems that the problem is scanf could it be the program im using (im using Miracle C)???
 
Yeah, it sure looks like the compiler. You might have to use some other one. Or might have to dig deep to find out if you need to specify some options out there.

Just to do a sanity check, i compiled this with windows &quot;cl&quot; compiler just fine.

Try this. Dont use scanf and just have the 2 printfs. See if your compiler likes that...

Cheers
KK IBM MQSeries Specialist
 
o i tried that alrdy and it works so i alrdy have confirmed that it is scanf, im not gonna use miracle c then its not worth the hassil but could u tell me that is windows cl and can i use that and where can i get this?
 
You get &quot;cl&quot; compiler when you install Visual C++. You could use any compiler that comes with products such as boorland c++ etc.

Cheers.
KK IBM MQSeries Specialist
 
i had visual c++ but i lost it any chance i can get a free or get a trial compiler?
 
hey you been really help full and if i can use ur brain onec more.... here what i came across the program (miracle c) has a example program similar to mine
#include <stdio.h>

void main()
{
unsigned n;

printf(&quot;type a number: &quot;);
scanf(&quot;%i&quot;,&n);
printf(&quot;number %d is %x hex&quot;,n,n);

return;
}
im assuming unsigned n is something i need, what does it mean?
 
even though this seems similar, it isnt. Because this example is using integers. unsigned is a keyword in &quot;C&quot; that you can use to define variables of different types you want to have. Basically it toggles between signs. + and -. Default is signed which means you can enter -100 or +100 for your variable &quot;n&quot;. Whereas you cannot when you specify unsigned. ONly +ve values.

This has nothing to do with what you were trying to do.

Cheers.
KK IBM MQSeries Specialist
 
ooo sry. i got a new compiler called digital mars some sent me im going to go try that ::Fingers Crossed::
 
o that was you in the other thread man you ever where
 
Hi VspecGTR3.
Just an improvement to your program. Read the code below:
Code:
#include <stdio.h>

void main()
{
  char *me = &quot; &quot;;
  printf(&quot;What is your name?&quot;);
  scanf(&quot;%s&quot;,me);
  printf(&quot;Darn glad to meet you, %s!\n&quot;,me);
}
As you see most of the code is the same. The only difference is the variable me. Instead of using a char array I have used a pointer. This has the benefit of having unlimited string length (not including spaces in scanf). Notice the declaring of the pointer assigning &quot; &quot;. This is to avoid null pointer warning.
Hope that helps. Adonai :)
 
vspecgtr3,

This is wrong:
char buf[30];
scanf(&quot; %s&quot;, &buf);

A string is a char array. The memaddress of each element
can be accessed with the notation &array[number].
Assigning chars to the array is done by passing the first
address and filling in the array.
char buf[30];
scanf(&quot; %s&quot;, buf);

Consider using fgets instead of scanf.
Scanf does no bounds checking, and though usable
by the expert, is not suitable for beginners IMO.


Adholioshake,
/*#include <stdio.h>

void main()*/

This is deprecated. The environment has no way to
indicate program failure or success using this.
Try int main(void).


/*{
char *me = &quot; &quot;;*/

This invokes undefined behavior when trying to
use it as a char array of 'n' elements.
It is also, IMO, very bad style.
char me[30];
or:
char *me = malloc(30 * sizeof(char));
actually allocates memory and creates a usable
array.

/*printf(&quot;What is your name?&quot;);
scanf(&quot;%s&quot;,me);
printf(&quot;Darn glad to meet you, %s!\n&quot;,me);
}*/

 
Just to reiterate, your original example should work, and on any compiler, if you use &quot;me&quot; instead of &quot;&me&quot;.

The error says that you applied &quot;&&quot;, the address-of operator, to a non-lvalue (in this case, &quot;me&quot;). Don't mind what that means, exactly, but you can't do that. Since &quot;me&quot; is a character array, it's an address on its own; taking the address of an address makes no sense (in this case).

Nine times out of ten, it's not the compiler's fault, but yours.
 
Chipper,
You are right in that me should work fine and no need to use &me. But you are not quite right when you said &quot;you can't do that&quot;. Reason being this.

When you define a char me[10], it defines a stack with me[0] being the top of the array and is a pointer to the memory assigned to me[0]. Say, the memory location(physical) allocated to me[0] be 9988. me[0] would be 9989, since its a char array(dont want to go into more details here).

Now when you say, scanf(me), it tells the program to store the input from stdin starting at location 9988. Since me itself is a pointer.

But when you say scanf(&me). It goes and searches the value in &me, which would be nothing but 9988. &me though is a different location and it could be 7766, may be.

Example of &me is similar to int x, and you specifying scanf(&x).

Hope this helps.

Cheers.
KK IBM MQSeries Specialist
 
I wish i understood what ythe discrpency is over but i downloaded visual c++ standard edition and compiled and builded the exe and it works fine it was my compiler all along. but my new problem is (dont know if its compiler or what not)

#include <stdio.h>

void main()
{
char me[20];

printf(&quot;What is your name?&quot;);
scanf(&quot;%s&quot;,&me);
printf(&quot;Darn glad to meet you, %s!\n&quot;,me);
}

this doesnt seem to work
 
Am afraid, you again missed out the point. whats the problem this time.

As for compiling, building and running this sample with visual c++ is concerned, i would bet it HAS TO work. Sanity check again, i tested it and it works fine for me. Not sure whats the issue with you.

Cheers.
KK IBM MQSeries Specialist
 
mqonnet thanks for your patients and help, this forum has the nices people i swear thanks again mqonnet.
 
hey mqonnet i rewrote the program and everything and it builds the .exe perfectly except....

printf(&quot;What is you name?&quot;);
scanf(&quot;%s&quot;,&me);
printf(&quot;Darn Glad to meet you, $s!\n&quot;,me);

i enter my name (eric) but it doesn't say
&quot;Darn Glad to meet you, eric&quot;
in fact all it does is the MSdos screen goes away.
 
but if i run the program with in visual c++ everything shows and it doesnt close the window infact it ask me to hit anykey
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top