Welcome to HBH! If you have tried to register and didn't get a verification email, please using the following link to resend the verification email.

Compiler compatibility


ghost's Avatar
0 0

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.


ghost's Avatar
0 0

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.


spyware's Avatar
Banned
0 0

MoshBat wrote: It really isn't.

It is, programming in assembly is plain stupid, and dealing with flip-flops is just crazy talk.

C, the backbone? I'll sign that petition.


ghost's Avatar
0 0

I use Dev C++ on my laptop (which has vista on it) all the time so I don't think that is your problem.


ghost's Avatar
0 0

that's exactly what I'm using. It works with both C and C++?


ghost's Avatar
0 0

sharline23 wrote: that's exactly what I'm using. It works with both C and C++?

I don't code in C on my laptop (or at all for that matter) but I don't see why C++ would work and C wouldn't.


ghost's Avatar
0 0

sharline23 wrote: It works with both C and C++? Well it damn well should, valid C code is generally valid C++ code. Which means that C code should compile fine in a C++ compiler.


ghost's Avatar
0 0

I finally figured it out.

For whatever reason the getchar() function wasn't working right so I ended up using system( "pause" ) and everything works perfectly.


ghost's Avatar
0 0

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.


ghost's Avatar
0 0
#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);


hellboundhackersok's Avatar
Banned
0 0

sharline23 wrote: I finally figured it out.

For whatever reason the getchar() function wasn't working right so I ended up using system("pause") and everything works perfectly.

cin.get(); cin.get();

will work just as well.


richohealey's Avatar
Python Ninja
0 0

Urgh… system("pause") … how many times does this need to be said.

Calling the pause binary is stupid because:

  1. It breaks on old NT systems
  2. It breaks on unix systems
  3. It breaks on broken win32 platforms.
  4. It's an unneccesary waste of resources to do something trivial.

ghost's Avatar
0 0

Why not use getch()??? just include<conio.h>….and instead of getchar(); put getch(); getchar() inputs your line feed(its present in enter….ascii code 10) too… and C is (at least acc. to me) the backbone of programming….and Richohealey is right..I think you know what the system command does..