Thursday, July 30, 2009

How come my C++ codes work in Dev and not in Microsoft Visual express?

theres nothing wrong cuz it works in dev





here it is


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


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





main()


{


int passes = 0, failures = 0, student = 1, result;





/* process 10 students; counter-controllede loop*/


while (student %26lt;= 10) {


printf("Enter result (1=pass,2=fail): ");


scanf("%d", %26amp;result);





if (result == 1)


passes = passes + 1;


else


failures = failures + 1;





student = student + 1;


}


printf("Passed %d\n", passes);


printf("Failed %d\n", failures);





if (passes %26gt; 8)


printf("Raise tuition\n");


else


printf("you have dumbass in your school\n");





getch();


return 0;


}

How come my C++ codes work in Dev and not in Microsoft Visual express?
it could have some syntax errors that are only readable in Visual express... try to code it in a different way and try again
Reply:Hm, looks a lot more like C code than C++ code. You may want to consider using %26lt;iostream%26gt; and cin/cout for input/output, as Visual Express is for C++, not C. Your errors are probably cause by your un-C++-ness





main() should be int main().


Not really an error, but you should use the ++ operator, ie


foo++; not


foo = foo + 1;


cout %26lt;%26lt; "foo"; not


printf("foo");


cin.get(); not


getch();


And it is considerably easier to use // for line comments rather than /* */
Reply:Did your teacher tell you that it had to compile in Microsoft Visual Express? Different compilers pick up different errors. I always progammed my C++ programs in Dev, and my teacher never had a problem compiling them.





One hint, though. You've programmed it so that anything other than a 1 is a fail. This means that numbers such as 3,4,5,0,etc, are all considered to be fails instead of incorrect input. I don't know how strict your teacher is, but this could be considered to be an error.

daylily

No comments:

Post a Comment