Thursday, July 30, 2009

Why is there an error in C?

#include "stdafx.h





int main(int argc, char* argv[])


{


int c;





c = getchar();


while (c != EOF) {


putchar(c);


c = getchar();


}


}








That is my code. when I run it, it shows an error. I use Microsoft Visual C++, Windows XP, and I am making a Console Application. A little help please...

Why is there an error in C?
you are missing a closing quote on the include





#include "stdafx.h"
Reply:getchar(); is an undeclared identifier. What do you want to do with getchar(); ? What will getchar be? A number, a letter, a string, what? Identify it. Just like int c; you need to indentify getchar(); - Without quotations it becomes a variable, but the variable is undeclared. When in doubt, find out what type of program you are making and look for it on the internet. You can find alot of C++ programming examples. Just type around. You'll find the answer. In C++, one error leads to another. Tip: Build your application after each line of code so that you may find your errors as you go along. Instead you are writing a program with errors from the start. When you fix one error, another will pop up. Start over and build from beginning. Don't just code away.
Reply:You are missing a closing speech mark (") on your first line, so C thinks your whole file is part of the filename to include! (You are probably getting an EOF error).





Maybe this is more what you want (I'm assuming your header file stdafx.h contains the standard input/output library stdio.h):





---





#include %26lt;stdio.h%26gt;





int main(int argc, char* argv[])


{


int c;





while ( (c=getchar()) != EOF) {


putchar(c);


}





return 0;


}





---





You might also find this website useful (I do):


http://www.cppreference.com/index.html
Reply:lets see my answer is....................... get a life
Reply:I think you selected the wrong options while creating your project.





Why don't you try creating a new project and then copy and paste your main into the new project, and then try running it. ( and add the header )





The steps for creating a project are:


-New Project


-select C++ , win 32 on left side and Win 32 Console project on the right side


-select location where to create , give name


-select Application Settings, click on Console and Empty,


-Finish











Oh, and another thing is that you don't have a return statement in your main. You always need to have a return statement in your main function!!!! Since, you said int main, your main should return an integer and so you could put something like a return 0; as the last statement in your main.





Hope this fixes your problem.
Reply:! = i think





see what happens...





if not take the damn P.C. back to the shop and tell them you are to thick to own a p.c.!





LoL! I wish you luck... i have not got a clue... cheers!





I guess you got Cods Heads there! hope these kind people help!


No comments:

Post a Comment