Go4Expert (http://www.go4expert.com/)
-   C (http://www.go4expert.com/forums/c/)
-   -   code not working (http://www.go4expert.com/forums/code-not-working-t19204/)

Gura_Kid 28Aug2009 05:59

code not working
I'm trying to solve TOO + TOO + TOO + TOO = GOOD using a c++ program.

My solution to this, is to use a nested loop for each unique letter (in this case T, O, G, D). The loops would systematically assign the digits from 0-9 to each letter.

However the following program gives me the wrong results :crazy:


#include <iostream>
using namespace std;

int main() {
  int T, O, G, D;
  for (T=0;T<=9;T++) {
      for (O=0;O<=9;O++) {
        for (G=0;G<=9;G++) {
            for (D=0;D<=9;D++) {
              if ( (T*100 + O*10 + O) * 4 == (G*1000)+(O*100)+(O*10)+D) {
                  cout << "T=" << T << "\nO=" << O << "\nG=" << G << "\nD=" << D << endl;
  cout << "Press <ENTER> to exit. ";
  return 0;

xpi0t0s 28Aug2009 11:59

Re: code not working
How are the results wrong?
What results did you get, and what results did you expect?
Is a requirement that T,O,G,D are all unique? If so, then you haven't coded this into your program.
Is a requirement that neither of TOO and GOOD begin with zero? Similarly you haven't coded that in.

All times are GMT +5.5. The time now is 10:26.