I'm new; can someone take a look at this?

I just started learning c++ this week, and as such, I dont yet have a great understanding of really anything xD. I know that there are errors galore in this program, but i dont really know where to look, as the build output isnt helping at all. I'd appreciate any feedback whatsoever =D
(btw, its supposed to be something of a calculator program in win32 console)


// uniq1.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"


int _tmain(int argc, _TCHAR* argv[])
{
return 0;
}


#include <iostream>
int main()
{
using namespace std;


float x,z;
int y;
cout<< "Enter a decimal x" <<endl;
cout<< "x: " ;
cin>> x <<endl;
cout<< "Enter the number that corresponds to the operation you would like to use" <<endl;
cou<<t "1. +" <<endl;
cout<< "2. -" <<endl;
cout<< "3. *" <<endl;
cout<< "4. /" <<endl;
cout<< "5. % (modulus)"<< endl;
cin>> y <<endl;
cout<< "Now enter the factor by which x will be multiplied/added to/ subtracted by etc...";
cout<< "z: ";
cin>> z;
if ( y == 1 )
{ cout<< x+z;
}

else if (y == 2)
{cout<< x-z;
}
else if (y == 3)
{cout<< x*z;
}
else if (y == 4)
{cout<< x/z;
}
else if (y == 5)
{ cout<< x%z;
}
else
{cout<< "You have entered an invalid operation value choice.";

}

return 0;
}
Last edited on
1) Please use code tags:

[code]
Put your code here
[/code]

It makes it infinitely easier to read your code.

2) The build output may not help you yet, but it helps us. Give us the errors you're getting so we can explain to you what they mean and how to fix them. Makes it easier to find and fix the problem.

3) don't use << endl with cin.

4) don't have _tmain() and main() in the same program -- pick one. I recommend using main() (_tmain is a silly nonstandard extension)


That's all I can see off the bat. Like I say an error list would help.
You've got anerror on the line:

cou<<t "1. +" << endl; // the t is in the wrong place

Can't see any ofthe others that Disch hasn't already pointed out.
Thanks guys; omitting the <<endl's with the cins fixed the like 80-some errors xD

Here's a final version
(comments, questions etc are welcome =D)

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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
// uniq1.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"


int _tmain(int argc, _TCHAR* argv[])
{
	return 0;
}


#include <iostream>
int main()
{
	using namespace std;
	

	float x,z;
	int y;
	cout<< "Enter a decimal x" <<endl;
	cout<< "x:  " ;
	cin>> x ;
	cout<< "Enter the number that corresponds to the operation you would like to use" <<endl;
	cout<< "1. +" <<endl; 
		cout<< "2. - (will find the positive difference)" <<endl; 
		cout<< "3. *" <<endl;
		cout<< "4. /" <<endl;
		//cout<< "5. %  "<< endl; (I took this one out cos you can't use modulus with floats)
	cin>> y ;
	cout<< "Now enter the factor by which x will be multiplied/added to/ subtracted by etc...";
		cout<< "z:  ";
		cin>> z; 
	if ( y == 1 )
	{ cout<< x+z <<endl;
	}

	else if (y == 2)
	{
		if (x>=z)
		{cout<< x-z <<endl;}
		else
	cout<< z-x <<endl;}
	else if (y == 3)
	{cout<< x*z <<endl;
	}
	else if (y == 4)
	{cout<< x/z <<endl;
	}
	/*else if (y == 5)
	{ cout<< x%z;
	}
(I took this one out cos you can't use modulus with floats)*/
	else
	{cout<< "You have entered an invalid operation value choice." <<endl;
	
	}
	
	return 0;
}
Personally I don't like your Syntax and it will get very difficult to read if u have some thousands lines of code. Us something like this:
1
2
3
4
5
6
7
8
9
10
11
if(a == b){
  if(c == d){
    print("c");
  }else if(c == a){
    print("b");
  }else{
    print("a");
  }
}else{
  print("x");
}

or like this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
if(a == b)
{
  if(c == d)
  {
    print("c");
  }
  else if(c == a)
  {
    print("b");
  }
  else
  {
    print("a");
  }
}
else
{
  print("x");
}


For absolute value of an integer
1
2
3
4
if (x>=z)
		{cout<< x-z <<endl;}
		else
	cout<< z-x <<endl;}

there are some little abbreviations, which save you the jump. Here an example:
cout<< (((x - z) >> 31) ^ (x - z)) <<endl;
You can also just use abs() >_>
/*else if (y == 5)
{ cout<< x%z;
}


You can use:

cout << (int)x % (int)z;

Which, in a sense, temporarily converts the 2 values to type int, which will work with modulus.
Topic archived. No new replies allowed.