Completed a Program but Kattis thinks it's wrong?

Please Note: While my class has assigned Kattis problems before, I am completing this program on Kattis for my own practice, not for an assignment.

I am doing this problem: https://open.kattis.com/problems/pot
and have successfully gotten the correct output for all of the example inputs/outputs on the Kattis page. However, when I try submitting it to Kattis it says my answer is wrong. What am I doing wrong? I've looked it over thoroughly but still can't find what I'm doing wrong.

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
/*
Program Name: pot_v2.cpp
Version: 2
Author: PiggiesGoSqueal
Kattis Link: https://open.kattis.com/problems/pot

Algorithm Steps:
1. Ask for n (the number of numbers that will be input).
2. Ask for first number and assign it to a string variable (userInput).
  - Will take last digit in number and assign it to a variable
    called "powerStr" then will delete it from userInput.
  - Will convert powerStr to power (int var)
  - Will convert userInput to an integer variable (userInputInt)
  - Will add (userInputInt ^ power) to total variable
3. Repeats step 2 for all numbers.
4. Prints total.

Status: Completed

Difference between V1 and V2:
- Code Cleanup
*/
#include <iostream>
#include <math.h>
#include <string>

int main() {
    int n;
    std::string userInput, powerStr;
    int userInputInt, power, total;

    // Asks for how many numbers user will input:
    std::cin >> n;

    // for as many numbers the user will input:
    for (int i = 0; i < n; i++) {
        std::cin >> userInput;

        // assigns the last char in userInput to lastChar variable
        powerStr = userInput.at(userInput.size()-1);

        // converts powerStr to an int variable named "power"
        power = stoi(powerStr);

        // removes last char from string.
        userInput.pop_back();

        // Adds userInputInt^power to total.
        userInputInt = stoi(userInput);
        total += pow(userInputInt, power);
    }
    total -= 1; // subtracts one to get answer

    // I have tried both of the following outputs but
    // Kattis considers them both to be incorrect:
    std::cout << total;
    //std::cout << total << '\n';

    return 0;
}


Help would be greatly appreciated, thank you!
Why do you subtract 1 from total at line 52?

I think the problem is that total is uninitialized.
@dhayden I subtract one because I noticed when I input my answers, it was always 1 above the correct answer (based on Kattis output examples). So by subtracting one, it made it the correct answer for all 3 sample problems provided by Kattis.

As for it being uninitialized, what do you mean? I briefly read over this post:
http://www.cplusplus.com/forum/general/62807/

Then tried having my variables be declared like so:
1
2
3
4
    int n = 0;
    std::string userInput, powerStr;
    int userInputInt, power;
    int total = 0;


However, that still didn't work.
Last edited on
@dhayden is correct. Remove line 52 and initialize total = 0;.

Click on the little gear by you posted code and it will take you to an online compiler. Go ahead and make those changes and test your code.
@doug4 Oh okay, thanks for clarifying.

Didn't realize there was an online compiler you could use too!

Thank you both. It works.

Edit: Ohhhh I just realized why you are supposed to initialize it now. I'm a bit slow haha, thank you.
Last edited on
Topic archived. No new replies allowed.