You can laugh at me but.....

I am brand new to learning C++. I was hoping that anyone that was willing to compare these two sets of code could:

1. Let me know if my approach is just totally wrong.
2. Could it possibly work if anything was slightly changed?
3. What tips would help me in the future to go in the right direction?

(Using code blocks, my program compiled, but when run it did nothing except say press ant key to continue.)

Thanks in advance.

my code:

#include <iostream>

double dNumber()
{
using namespace std;
cout << "Enter a number with a decimal point:";
double x;
cin >> x;
return x;
}

char cSymbol ()
{
using namespace std;
cout << "Enter a symbol to perform that mathimatical task: + , - , * , or / :";
char x;
cin >> x;
return x;
}


int main()
{
using namespace std;
double dNumber(int x);
double dNumber(int y);
char cSymbol(char z);
int x;
int y;
char z;
if (z == '+')
cout << x + y << endl;
if (z == '-')
cout << x - y << endl;
if (z == '*')
cout << x * y << endl;
if (z == '/')
cout << x / y << endl;
return 0;
}


the right code:


#include <iostream>

int main()
{
using namespace std;
cout << "Enter a value: ";
double dX;
cin >> dX;
cout << "Enter a second value: ";
double dY;
cin >> dY;
cout << "Enter one of the following: +, -, *, or /";
char chChoice;
cin >> chChoice;
if (chChoice == '+')
cout << dX << " + " << dY << " is " << dX + dY << endl;
if (chChoice == '-')
cout << dX << " - " << dY << " is " << dX - dY << endl;
if (chChoice == '*')
cout << dX << " * " << dY << " is " << dX * dY << endl;
if (chChoice == '/')
cout << dX << " / " << dY << " is " << dX / dY << endl;
return 0;





the first obvious thing i noticed is that youre typing
 
using namespace std

in every function where you can just place it at the top once after your preprocessor directives. also your "right" code is missing a curly brace at the end.

as for the wrong code youre using variables that you havent defined yet, and youre calling the functions and giving them parameters but there werent any parameters when you defined them.
you need to initialize all of your variables first and when you call them i think you meant to do something like this:
1
2
3
double x = dNumber();
double y = dNumber();
char z = cSymbol();


lastly use code tags by pressing the <> button under format and try to use more descriptive titles than "You can laugh at me but..." so people will understand the problem better.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#include <iostream>
using namespace std;

double get_num();
char cSymbol();

int main(){
	double num1, num2;
	char z;
	cout << "Enter first number with a decimal point :"<<endl;
	num1 = get_num();
	cout << "Enter a second decimal number :"<<endl;
	num2 = get_num();
	z = cSymbol();

	if (z == '+')
		cout << num1 + num2 << endl;
	if (z == '-')
		cout << num1 - num2 << endl;
	if (z == '*')
		cout << num1 * num2 << endl;
	if (z == '/')
		cout << num1 / num2 << endl;
	return 0;
}

double get_num(){
	double num;
	cin >> num;
	return num;
}
char cSymbol (){
	char x;
	cout << "Enter a symbol to perform that mathimatical task: + , - , * , or / :";
	cin >> x;
	return x;
}


this is how you should have written your first code... using functions... let me know if you have any questions on this
Last edited on
Topic archived. No new replies allowed.