Well, please excuse my noobiness, and my little skill with C++, but give me a break, I'm young.

Not for homework, just for the sake of being able to do it,I was trying to make a code for the Pythagorean theorem, and here is the start of the code (of course not working or else I would't have made this thread) to find the basic a^2+b^2=c^2. Please tell me what I did wrong, and as I said, excuse my unfamiliarity to C++! Note: this is only a code snippet of whats important to the code.

int a;
int b;
int c;

float choice;
float choice1 = 1;
float choice2 = 2;
float choice3 = 3;
int a2 = a^2;
int b2 = b^2;
int c2 = c^2;
int awnser = a^2 || b^2 || c^2;



                float pow();
		float sqrt();
		
		cout << "Enter a and b as 'a,b'\n";
		cin >> a,b; 
		cout << a << '^' << 2 << '+' << b << '^' << 2 << '=' << c << '^' << 2,a2 && b2;   
		c2 = a2 + b2;
		awnser = sqrt(c2);
		cout << awnser;

Recommended Answers

All 8 Replies

Very sorry to tell you but the operator ^, which you use e.g. a2=a^2 is actually the XOR operator. ie. it compares the bits in the value and sets them according to the rule:

A  B  Out
0  0   0
1  0   1
0  1   1
1  1   0

Thus 1 ^ 2 == 3, 2^2 is 2, and 2^3 is 1. [You can check that by outputting a2,b2 etc.

So what you really needed was the pow function e.g. a2=pow(a,2); or to just multiply the two numbers together, e.g. a2=a*a; .

I note that you also use the comma and && operator in your output. I very much doubt that you want that. e.g. your cin line should be cin>>a>>b; .

What I recommend is that you put a lot of cout<<"Variable == "<<var<<endl; after EACH statement, to see what changes etc.

thanks, it now compiles. But for some reason It now outputs (If I entered 6,4) "6^2 + 0^2 = 0^20!

BTW I edited the code to:

#include <cmath>
#include <iostream>
#include <complex>
#include <valarray> 
#include <stdio.h>
using namespace std;
double pow();
double pow();
double sqrt();
double a;
double b;
double c;

float choice;
float choice1 = 1;
float choice2 = 2;
float choice3 = 3;
double a2= pow(a,2);
double b2= pow(b,2);
double c2= pow(b,2);
double answerc = sqrt (c2);
double answerb = sqrt (b2);
double answera = sqrt (a2);

cout << "Enter a and b as 'a,b'\n";
		cin>>a>>b;  
		cout << a << '^' << 2 << '+' << b << '^' << 2 << '=' << c << '^' << 2;    
		c2 = a2 + b2;
		
		cout << answerc;
		system ("pause");

It is a good practice to not use the system("pause"), the reason is that this will only work on a windows, and if you take it to another computer it won't work. Try using a fflush(stdin); followed by cin.get();

That was the reason I wanted you to put an output after EVERY statement.

You have not yet understood the ordering issue in a program. For example, consider the variable c2. First it is created on line 20, It is assigned the value pow(b,2).
However, at that point b could be ANYTHING. That is because it had not been assigned a value YET. Thus c2 can be ANYTHING. Then later c2 is set to a2+b2. The assignment of a and b in line 26, is irrelevant. a2 and b2 have already been set, and are not re-set.

Please go through and write a cout<<"Variable == "<<var<<endl; at each line.

Programs are executed in an order, statement after statement, if something changes later, that does not effect the statement that has been executed. It is not a maths proof.

You need to rearrange your code, so that a,b,c are read from the input, before a2,b2 and c2 are evaluated, and before answerc is evaluated and printed to the screen

thanks, I got it to work to find a, b, and c

It is a good practice to not use the system("pause"), the reason is that this will only work on a windows, and if you take it to another computer it won't work. Try using a fflush(stdin); followed by cin.get();

It's also a bad practice to use fflush(stdin) because it's a legal operation -- you cannot flush an input buffer.

any 1 has solution for it:, In the triangle Pythagoras theorem is used to find out the length of any in known side
If the base is 4 cm and height is 7cm find out the hypotheses by using the Pythagoras
(Hypotenuse) 2 = (base) 2+ (perpendicular )2


, i need C++ code of it without header file of <math.h> , need to put only conio.h ,iostream.h header file only,??

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.