Help with a For Loop Program Beginner

Hello,
I posted this and saw it on the board, but it disappeared so I'm going to try again. My apologies if someone has it down to try and help me. I am trying to write a program for homework. For the first part of the program I have to take 4 digit numbers, add the first 2 and last 2 digits, square them and compare to the original number. I'm fine on that and the first part of the program works. I'm very new to C++, and appreciate any help ( In the 5th week of beginers C++ class)

For the second half I have to take 2,3,and 4 digit numbers, cube their digits, add them back to the original number and compare. I copied the for loop that worked previously, changed what I needed to, ran a test, and it didn't work. I took the cubing for loop out and ran it separately and verified the argument worked without the for loop by making it cout the separate digits and the sum of the cubes. I did it only with 3 digit numbers because I know there are 4 of them that fulfill the requirements. Could someone please check my program and help me to understand what I'm doing wrong that the 2nd for loop won't work by itself, or with the first for loop, which works fine.
Thanks

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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
/* Program Name:   doubler.cpp

   Description: 

   Date: 02/06/08 	Dev-C++
*/
//********************************** Includes
#include <cfloat>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <cctype>
#include <iomanip>
#include <cmath>
#include <fstream>
#include <string>
#include <ctime>
#include <conio.h>
#include <windows.h>

#define cls system("cls")
#define frz system("pause");
#define yl  system("color 0e");

using namespace std;

//********************************** Type definitions

//********************************** Function Prototypes

//********************************** Main Function



int main()
{
    time_t t;
	time(&t);
	yl;
	cls;
	
    int num, num1, first, last, square, dig4, dig3, dig2, dig1, scube; 
   
   
   
   cout << " doubler.cpp " << ctime(&t) << endl << endl;
   
         for (num1=1000;num1<=9999;num1++)
      {
          first = num1 / 100;
          last = num1 % 100;
          square =  (first + last) * (first + last); 
          if(num1==square)
             {
             cout << "The number "  << num1 << " meets the criteria for " ;
             cout << "section 1  ";
             cout << endl;  
             }//end if
             }//end for
             
    
    for (num=100; num<=999; num++)
    
      {        
          dig3 = num % 10;
          num /= 10;
          dig2 = num % 10;
          dig1 = num /10;
          scube = (dig1*dig1*dig1)+(dig2*dig2*dig2)+(dig3*dig3*dig3);
          
          if (num==scube)
             {
             cout << "The number "  << num << " meets the criteria for " ;
             cout << "section 2  ";
             }//end if
             }//end for
    
          
 
 
	frz;
	return 0;
} // end of main function

//********************************** Function Definitions 
First off, you've got a bunch of stuff that I don't think needs to be there. Shoot me and e-mail and I'll see if I can't help ya out. I'll need to ask you some more rather in-depth questions, and rather than clog the board, e-mailing would be much more efficient for this.

amrcn@uga.edu
But then nobody is learning something out of it. And that is one reason for having
a forum.
Your question:
I think you calculate with floats in part 2. You declared int. Or am I wrong?
Last edited on
Hello,
Just to let everyone know. I also emailed my class prof to ask for help (He's not the amrcn.uga.edu person). The prof of my class emailed me back to use a temp variable instead of num for digitizing. This is because in the calculations change num every time it gets divided. For example temp=num; before the dig3 = statement, and then replace all of the nums with temp so that at the end you can compare the scube (sum of the cubes) with the original number (num). I did it and it worked for me.

Thanks for the help from eeveryone!
Last edited on
Topic archived. No new replies allowed.