Compiler compatibility
Just a quick question about C…
Is bloodshed compatible with vista? Although I was originally going to learn Perl my friend convinced me to learn C and C++ first.
I'm getting some strange errors though. take this code for example…
int mult ( int x, int y );
int main()
{
int x;
int y;
printf( "Please input two numbers to be multiplied: " );
scanf( "%d", &x );
scanf( "%d", &y );
printf( "The product of your two numbers is %d\n", mult( x, y ) );
getchar();
}
int mult (int x, int y)
{
return x * y;
}
When I compile and run it, (which i have done seperately and combined) It gives the first printf. But when I put in two numbers spaced or not, the program just exits. Its been doing this for all of my simple programs thus far. Although I did input getchar(); for another program and the program half worked, but I dont want to have to do this for all my programs.
I should also mention that the code above is merely an example from a tutorial. Should I just skim the C tutorial and move on to C++? According to my friend, C is the backbone for other programming languages which is why he wanted me to learn it.
No, its the same before I compile it, if it runs at all.
My friend is kind of an arrogant idiot, but he's my only live source of advice.
What's the exact syntax for running it from command prompt? Because everything I put does nothing.
I'm 80% sure that its vista, but I'll try on a different computer later to see if I get the same errors.
I will assume you are running the program directly from Dev-C++. If you were to run it directly from the command line (the executable image generated or not) you won't really have this issue, you will still see the output. Assuming you type the following at the prompt:
"50 10"
And hit enter, the program will do the following: Take 50 and toss it into your first variable, 10 into the next and then stop taking characters from the input stream. What does this mean? Well, you hit Enter so there is still another character in the input stream! "\n" to be exact.
If you were to run this from Dev-C++ the 'back box' will just disappear due to the fact, well, the program has printed what it needs to and the program is done. But you didn't get to see your output you say? Well, the "getChar()" call at the end, it ran just as you expected it to. It consumed the newline character! If you really wanted to be persistent, you can simply just use another "getChar()" at the end of your program.
Some other compilers would do something a bit different however, they will run your program followed by a "pause" command to 'cmd.' You can do the same in your program if you wish. Replace "getChar()" with 'system("pause");' NOTE: This is going to be OS dependant, but if it fixes the program for now, you can see other ways to address the issue as you learn.
This may or may not be the issue depending on how you invoke your program, but who knows, it might help. Good luck with your studies, pm if you need anything.
#include <iostream.h>
int mult (int , int); //Function Prototype
void main()
{
int num1,num2;
cout << "Please input two numbers to be multiplied: ";
cin >> num1;
cin >> num2;
cout << "The product of your two numbers is: "<< mult( num1 , num2 ) << endl;
cin.ignore(cin.rdbuf()->in_avail() + 1);
} //End of main()
int mult (int x, int y) //Function Defenition
{
return x * y;
}
Well, that's how I implement this in C++… It is compiled using Microsoft Visual C++ 6.0.
This will solve the break problem:
markupcin.ignore(cin.rdbuf()->in_avail() + 1);