Problem:Loop is displaying error second time around

So this program is suppose to allow the user to enter a 5 digit number. I store that as a string to test it. The program also allows the user to enter how many different 5 digit values he/she wants to enter. I then display the digits individually with its square and cube. The program works fine first time around, until the second loop it starts displaying weird results. I'd really appreciate it if someone could give me guidance to what I did wrong. The code looked fine to me. 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
#include<iostream>
#include<math.h>

using namespace std;

int main(){
    string value;
    int i=0;
    int trys;
    int trials = 0;
    int trials2;
    
    
    cout << "How many trys will you enter:";
    cin >> trys;
    
    for ( int trials = 0; trials  < trys; trials++  ) {
          cout << "Enter the value:";
          cin >> value;
          
          trials2 = 0;
          
          if ( value.length() == 5  ) {
               while ( trials2 < 5){
               int z = value[i] - '0';
               cout << value[i] << "\t" << pow( z , 2) << pow (z , 3)<< endl;
               i++;
               trials2++;
               }   
          }
          
    }

    system("pause");
    return 0;
}
What is line 25 trying to do? It looks like it is trying to subtract a literal char from a item in a string array. This is a no no.

You increament trials2 in line 28 but then reset it to zero on line 21 when the loop comes back around? I don't get it.

Are you declaring 'trials' twice? Once on line 11 then again on line 17?

I'm surprized this compiled at all, your compiler should have caught that last one.

Change 'value' from a string type to a straight int array and that might help you a bit. You will need to change the way you feed them into that array as well.
I think you need to reset i to zero each time round.
You forgot to set i to zero, it will go out of bounds and you'll be using non-valid values.
Edit:
What Galik said.
Last edited on
Computergeek01 wrote:
I'm surprized this compiled at all, your compiler should have caught that last one.

It's not a compile time error because they have different scopes. It's obviously confusing and error prone, though.
The 'i' integer isn't used to control the loop in any way, if anything it's redundant he should just be using the 'trials' variable he set up earlier. Is there an internal loop I am missing somewhere? You two are making me feel a little dumb and this isn't even my program. Why didn't either of you address the fact that the OP is trying to use strings and chars for math operations? Even if this is allowed it's bad practice.

Never mind I see why he used the i integer, he still needs another loop to iterate through each array though
Last edited on
Thank you all so much! I forgot to set i back to zero. Yea, sorry my code is so messy, I'm just started learning C++. I'll definitely edit the code and make it more neat. Thanks a lot again!
Topic archived. No new replies allowed.