Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 A couple of questions about C?
Index -> Programming, C -> C Help
Goto page Previous  1, 2, 3, 4, 5
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
Vertico




PostPosted: Tue Jan 06, 2009 8:41 pm   Post subject: Re: A couple of questions about C?

If you look at your code compared to what I posted, there are a few differences.

First off, the line in which the three variables are declared needs to be separated. theres a little , between them if you look closely.

c:
int firstNumber = 3, secondNumber = 2, thirdNumber = 0;


Because you took out the , the compiler doesn't recognize that three different variables are being declared, and you get an error.

You also removed the return 0; at the end which is needed to tell the program that the code has been executed successfully. As well, you need to leave the opening and closing brackets { } around the code so that the compiler knows where main starts and ends.


Take another look at what I posted.
Sponsor
Sponsor
Sponsor
sponsor
php111




PostPosted: Tue Jan 06, 2009 9:01 pm   Post subject: Re: A couple of questions about C?

I fixed it and still get errors.



code:
#include <stdio.h>
int main()
{


int firstNumber = 3, secondNumber = 2, thirdNumber = 0;

printf("This is a test file \n");

thirdNumber = firstNumber + secondNumber

printf("%d plus %d \n", firstNumber, secondNumber, thirdNumber);

thirdNumber = firstNumber - secondNumber;

printf("%d subtract %d equals %d \n", firstNumber, secondNumber, thirdNumber);

return 0;

}





Quote:
H:/Documents and Settings/baseball> test1.c
ERROR: multiple operands together
ERROR: syntax error before or at line 12 in file test1.c
==>: printf("%d plus %d \n", firstNumber, secondNumber, thirdNumber);
BUG: printf("%d plus %d \n", firstNumber, secondNumber, thirdNumber)<== ???
ERROR: cannot execute command 'test1.c'
H:/Documents and Settings/baseball>


OneOffDriveByPoster




PostPosted: Tue Jan 06, 2009 10:01 pm   Post subject: Re: A couple of questions about C?

php111 @ Tue Jan 06, 2009 9:01 pm wrote:
Quote:
H:/Documents and Settings/baseball> test1.c
ERROR: multiple operands together
ERROR: syntax error before or at line 12 in file test1.c
==>: printf("%d plus %d \n", firstNumber, secondNumber, thirdNumber);
BUG: printf("%d plus %d \n", firstNumber, secondNumber, thirdNumber)<== ???
ERROR: cannot execute command 'test1.c'
H:/Documents and Settings/baseball>
The format string specifies two parameters, but you are giving it three.
Vertico




PostPosted: Tue Jan 06, 2009 10:14 pm   Post subject: Re: A couple of questions about C?

php111 @ Tue Jan 06, 2009 10:01 pm wrote:
I fixed it and still get errors.

Quote:
H:/Documents and Settings/baseball> test1.c
ERROR: multiple operands together
ERROR: syntax error before or at line 12 in file test1.c
==>: printf("%d plus %d \n", firstNumber, secondNumber, thirdNumber);
BUG: printf("%d plus %d \n", firstNumber, secondNumber, thirdNumber)<== ???
ERROR: cannot execute command 'test1.c'
H:/Documents and Settings/baseball>




The thing with programming is you need to understand your errors. When the screen screams ERROR at you, it usually has a reference to the problem. If you ever want to get far at this, you need to start looking at the errors you are getting and truly examine them. At this stage, they are very easy to understand and typically point to the correct point in the code.
Your error points you straight to the actual line with the error on it.

Can you see the difference between what you typed, and what I typed?
c:
printf("%d plus %d \n", firstNumber, secondNumber, thirdNumber);

c:
printf("%d plus %d equals %d. \n", firstNumber, secondNumber, thirdNumber);
php111




PostPosted: Wed Jan 07, 2009 10:09 am   Post subject: Re: A couple of questions about C?

I have been looking at your line. I put the equals in and still get errors. I see the line that the command tells me.












Quote:
ERROR: multiple operands together
ERROR: syntax error before or at line 12 in file H:\DOCUME~1\baseball\test1.c
==>: printf("%d plus %d equals %d. \n", firstNumber, secondNumber, thirdNumber
);
BUG: printf("%d plus %d equals %d. \n", firstNumber, secondNumber, thirdNumber
)<== ???
ERROR: cannot execute command 'H:\DOCUME~1\baseball\test1.c'
H:/Documents and Settings/baseball>






code:
#include <stdio.h>
int main()
{


int firstNumber = 3, secondNumber = 2, thirdNumber = 0;

printf("This is a test file \n");

thirdNumber = firstNumber + secondNumber

printf("%d plus %d equals %d. \n", firstNumber, secondNumber, thirdNumber);

thirdNumber = firstNumber - secondNumber;

printf("%d subtract %d equals %d \n", firstNumber, secondNumber, thirdNumber);

return 0;

}
DemonWasp




PostPosted: Wed Jan 07, 2009 11:03 am   Post subject: RE:A couple of questions about C?

You're missing a semicolon on the end of the line immediately after "printf ( "This is a test file \n" );".

Since that line lacks a closing semicolon, it figures that the next line is a continuation of the statement, and gets confused when it doesn't make any sense. That's why it's giving the syntax error there.
php111




PostPosted: Wed Jan 07, 2009 11:27 am   Post subject: Re: RE:A couple of questions about C?

DemonWasp @ Wed Jan 07, 2009 11:03 am wrote:
You're missing a semicolon on the end of the line immediately after "printf ( "This is a test file \n" );".

Since that line lacks a closing semicolon, it figures that the next line is a continuation of the statement, and gets confused when it doesn't make any sense. That's why it's giving the syntax error there.



What? I have the semicolon. What do you mean? printf("This is a test file \n");
wtd




PostPosted: Wed Jan 07, 2009 11:35 am   Post subject: RE:A couple of questions about C?

The line after that one.
Sponsor
Sponsor
Sponsor
sponsor
DemonWasp




PostPosted: Wed Jan 07, 2009 11:47 am   Post subject: RE:A couple of questions about C?

What wtd said.

This is a vital skill to any programmer, so pay attention: you need to look at EXACTLY what you've given the computer, not what you *think* you've given it. The two may be different in an incredibly subtle way (missing semicolon, less-than instead of less-than-or-equal-to, etc), but it could be the source of a huge problem.

When you encounter a problem like this, start reading the code exactly like the computer would. Start with the first line, and make sure it does exactly what you think it does. It's slow and tedious at first, but once you get the hang of it, it becomes much easier.
php111




PostPosted: Wed Jan 07, 2009 12:04 pm   Post subject: Re: A couple of questions about C?

I finally got the first code right.

The second code.


code:
/Getting integer input from user.

#include <stdio.h>

int main ()
{

        int firstNumber = 0;

        printf ("Enter in a integer : ");
       
       //scanf is used to get input from the user, then places the value into the variable firstNumber
        scanf("%d", &firstNumber);

        printf ("You entered in %d.\n", firstNumber);

       

        return 0;



Do I write exactly this one line, You entered in %d?
php111




PostPosted: Wed Jan 07, 2009 1:43 pm   Post subject: Re: A couple of questions about C?

I done it. 13

I gave the number 13. I am going to make a new thread for the errors I get with the book. Thank you everyone.
jain.rani




PostPosted: Mon Jul 13, 2009 10:42 pm   Post subject: Re: A couple of questions about C?

//this is simple c program for hello word.

#include<stdio.h>
int main(){
printf("Hello, word");
return 0;
}



Thanks
Display posts from previous:   
   Index -> Programming, C -> C Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 5 of 5  [ 72 Posts ]
Goto page Previous  1, 2, 3, 4, 5
Jump to:   


Style:  
Search: