I wrote this code that inputs employees salaries and distributes them to how much they made.
I need some help translating this code into C++

#include <stdio.h>

int main(){
	int sales[9]={0};
	double gross=0, percent=0, salary=0;
	int total=0;
	int counter=0;
	printf("For 5 peoples salary ranges, Enter the first gross\n");
	scanf("%d", &gross);
	percent=gross*0.09;
	salary=200+percent;

	while (counter!=5){
		if (salary>=200 && salary <= 299)
			sales[0]++;
		if (salary>=300 && salary<=399)
			sales[1]++;
		if (salary>400 && salary<=499)
			sales[2]++;
		if (salary>=500 && salary<=599)
			sales[3]++;
		if (salary>=600 && salary<=699)
			sales[4]++;
		if (salary>=700 && salary<=799)
			sales[5]++;
		if (salary>=800 && salary<=899)
			sales[6]++;
		if (salary>=900 && salary<=999)
			sales[7]++;
		if (salary>=1000)
			sales[8]++;

		printf("Enter the next gross\n");
		scanf("%d",&gross);
		counter++;
	}
    printf("%d are in 200-299,%d are in 300-399, %d are in 400-499, %d are in 500-599, %d are in 600-699, %d are in 700-799, %d are in 800-899, %d are in 900-999, %d are in the 1000 range\n", sales[0], sales[1], sales[2], sales[3], sales[4], sales[5], sales[6], sales[7], sales[8]);
	return 0;
}

There's nothing stopping your code from compiling as C++. If you really want to make it look more like C++, just replace scanf and printf with cin and cout. That's pretty much all you can do, as the program is extremely simple and doesn't use language specific features beyond I/O.

Not exactly, you need to map each of the concepts like employee to a class and informations associated with it will become data members of the class ,etc.

Can somebody show me if its that simple

>Can somebody show me if its that simple
Sure, change the extension of your source file from .c to .cpp, and compile. You're done. :icon_rolleyes:

>you need to map each of the concepts like employee to a class and
>informations associated with it will become data members of the class ,etc.
Not everything C++ has to have classes.

I don't know what kind of compiler you have but changing it from .c to cpp will not change the syntax. I know there not much work you just have to change the prinf and scanf and the %d to something but I tried all that and came up with alot of errors

#include <iostream>
using namespace std;

int main() {
	int sales[9]= { 0 };
	double gross=0, percent=0, salary=0;
	int total=0;
	int counter=0;
	cout << "For 5 peoples salary ranges, Enter the first gross" << endl;
	cin >> gross;
	percent=gross*0.09;
	salary=200+percent;
	while (counter!=5) {
		if (salary>=200 && salary <= 299)
			sales[0]++;
		if (salary>=300 && salary<=399)
			sales[1]++;
		if (salary>400 && salary<=499)
			sales[2]++;
		if (salary>=500 && salary<=599)
			sales[3]++;
		if (salary>=600 && salary<=699)
			sales[4]++;
		if (salary>=700 && salary<=799)
			sales[5]++;
		if (salary>=800 && salary<=899)
			sales[6]++;
		if (salary>=900 && salary<=999)
			sales[7]++;
		if (salary>=1000)
			sales[8]++;
		cout << "Enter the next gross" << endl;
		cin >> gross;
		counter++;
	}
	cout << sales[0] << " are in 200-299, " << sales[1] << " are in 300-399, " << sales[2] << " are in 400-499, " << sales[3] << " are in 500-599, " << sales[4] << " are in 600-699, " << sales[5] << " are in 700-799, " << sales[6] << " are in 800-799, " << sales[7] << " are in 900-799, " << sales[8] << " are in the 1000 range" << endl;

	return 0;
}

>I don't know what kind of compiler you have but changing it from .c to cpp will not change the syntax.
Duh. My point was that the code is C++ compilable as is. Your code is already C++, and no translation is necessary; your idea of C vs. C++ is skewed. So how about refining your question from "I want to translate this code into C++" into something more specific, like "I want to make use of classes", or "I want to use C++ specific I/O"?

>I don't know what kind of compiler you have but changing it from .c to cpp will not change the syntax.
Duh. My point was that the code is C++ compilable as is. Your code is already C++, and no translation is necessary; your idea of C vs. C++ is skewed. So how about refining your question from "I want to translate this code into C++" into something more specific, like "I want to make use of classes", or "I want to use C++ specific I/O"?

so this code is C++0x... wrong - it is only compilable to C++, but not C++ at all
C++ introduce new standards

>so this code is C++0x...
Yes, it is.

>wrong - it is only compilable to C++, but not C++ at all
Oh, so it compiles as C++, but it's not really C++. Yea, that makes a lot of sense. :icon_rolleyes:

>C++ introduce new standards
Somehow I don't believe you're quite as familiar with the C++ standard as I am. Perhaps you could point out chapter and verse that says any part of the OP's program is a constraint violation and requires a compile-time diagnostic.

dude, not any C code can be compile as C++
working code != good code

>dude, not any C code can be compile as C++
Yes, I'm aware of that. But we're not talking about any C code, we're talking about this C code, and this C code can certainly be compiled as C++.

>working code != good code
Technically, the code isn't working regardless of how you compile it. There are bugs that need to be fixed. I'd also raise issue with the deprecated status of the C-style header, and the general poor quality of the code. However, that doesn't change the fact that this program (assuming the bugs are fixed) conforms to the standard C++ rule set (and as a result the code is C++), which is what you seem to be arguing against.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.