Don’t know why this doesn’t work

The code in BOLD is supposed to calculate whether the sum of a number’s factors are less than, equal to or greater than the number passed in.

The code works up until the for loop. Then I get an error line (lldb). Any ideas on what the problem is?

I’m using Xcode.

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
/*
 Connor Williams
 Williams6.cpp
 CSC1610
 This is a program that will add the integer values of a string, mod them by 64 then output the catagorey of the number
 */

#include <iostream>
#include <string>
#include <cctype>

using namespace std;

enum IntDesignation {perfect, deficient, abundant};

//Prototype
int checkSum(string word);
IntDesignation catString(int sum);

int main()
{
    string word;
    int sum;
    cout <<"Please enter a string"<<endl;
    cin >> word;
    while (word != "exit")
    {
        sum = checkSum(word);
        cout <<"The checksum is "<<sum<<", which is "<<catString(sum)<<"."<<endl;
        cout <<"Please enter a string"<<endl;
        cin >> word;
    }
    return 0;
}

int checkSum(string word)
{
    int sum=0;
    int n = word.length();
    for (int i = 0;i<n;i++)
    {
        int c = (char) word.at(i);
        sum += c;
    }
    sum %= 64;
    return sum;
}

IntDesignation catString(int sum)
{
    int check = 0;
    for (int x=0;x<sum/2;x++)
    {
        if (sum%x==0)
            check = check + x;
    }
    
    if (check<sum)
        cout<<"deficient";
    if (check==sum)
        cout<<"perfect";
    if (check>sum)
        cout<<"abundant";
}
You can't use 0 as the second argument to %, which happens when x == 0.

You have also forgot to return something from catString function.
Thank’s figured it out :) But now when I have it return one of the IntDesignations it outputs the number,not the word. How do I dix this?
Topic archived. No new replies allowed.