Code isn't working.

I've been trying to figure out why this isn't working. I don't have access to the question, but hopefully my comments I left will help in understanding what I'm trying to do and accomplish.

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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
#include <iostream>
#include <cstdlib>
#include <cmath>
#include <ctime>
#include <string>

using namespace std;

void AddTen(int num)            //User-Defined Function for if num<10
{
if (num<10)
num =num + 10;
}
void isPrime(int num)            //User-Defined Function for is prime or not
{
bool Prime = NULL;
for(int i = 1;i<sqrt(num);i++)
{
if(num%i ==0)
{
Prime = true;
break;
}
}
if (Prime == NULL)
{
cout<<"This is a prime number"<<endl;
}
else if(Prime = true)
{
cout<<"This is not a prime number"<<endl;
}
}
int main()
{
int sum = 0;
int num = 0;
string choice = " ";
while(num < 10)            //Generate random number   —Making sure it is greater than 10
{
srand(time(0));
num = rand()%100;         // Generates random num <100
}
while(true)
{
cout<<"your number is "<<num<<endl;
cout<<"Choose an option: "<<endl;
cout<<"a. Double the number"<<endl;
cout<<"b. Reverse the digits of the number"<<endl;             //Present User with Options
cout<<"c. Raise the number to the power of 2, 3, 4"<<endl;
cout<<"d. Sum the digits of the number"<<endl;
cout<<"e. less than 2 digits or greater than 3"<<endl;
cout<<"f. exit program"<<endl;
cin>>choice;
if(choice == "a")
{
num = num*2;       // double the number
AddTen(num);
isPrime(num);                   //calling my functions
}
else if(choice == "b")
{
string result = "";
string StrRev = to_string(num);          //convert num to string
for (int j = StrRev.length(); j >= 0; j–)
{
string a = StrRev.substr(j,1);         //Use subsets
result = result + a;
}
num = stoi(result);
AddTen(num);
isPrime(num);
}
else if(choice == "c")
{
char powerChoice = ' ';
cout<<"Choose to raise to the power of:"<<endl;
cout<<"a. 2"<<endl;
cout<<"b. 3"<<endl;
cout<<"c. 4"<<endl;
cin>>powerChoice;
if(powerChoice == 'a')            //User chooses what power
{
num = pow(num, 2);
}
else if(powerChoice == 'b' )
{
num = pow(num, 3);
}
else if(powerChoice == 'c')
{
num = pow(num, 4);
}
AddTen(num);
isPrime(num);
}
else if(choice == 'd')
{
string str = to_string(num);            //convert to string
int holder = 0;
for(int i = 0; i < str.length();i++)
{
string y = str.substr(i,1);       //User subsets for individual digits
holder = stoi(y);         //Convert subset to numerical
sum = sum + holder;           //add sum
num = sum;
}
AddTen(num);
isPrime(num);
}
else if(choice == 'e')
{
string length = to_string(num);
if(length.length() == 2)           //Figures out whether 2 or 3 digits
{
string num1 = length.substr(0,1);
string num2 = length.substr(1,1);
int numb1 = stoi(num1);
int numb2 = stoi(num2);
num = pow(numb1, numb2);         //uses individual digits to set (num1)^(num2)
}
string substr = length.substr(length.length() – 1,1);
if(length.length() == 3 && substr.length() <= 4)
{
string num1 = length.substr(0,2);
string num2 = length.substr(2,1);
int numb1 = stoi(num1);
int numb2 = stoi(num2);
num = pow(numb1, numb2);          //Finds 2dig and 1dig numbers
}
AddTen(num);
isPrime(num);
}
else
{
break;          //User chooses to exit
}
cout<<"Your new number is "<<num<<endl;
}
return 0;
}
closed account (48T7M4Gy)
for (int j = StrRev.length(); j >= 0; j–)

There's one error.

It raises the question though. How come you haven't run this to find compiler errors?
closed account (48T7M4Gy)
else if(choice == "d")
Last edited on
closed account (48T7M4Gy)
A piece of advice: fix up the first error, run/compile, fix up the first error, run/compile and keep going until all errors are fixed. If you need help on an error then that's fine. Specify the line number, the error message.

Another observation and piece of advice is, since you probably don't have a pseudo-code plan for the project, type in a line or two maximum of code then run/compile/error check. Don't write great slabs of uncontrolled code you can't keep track of or narrow down what's causing trouble.
closed account (48T7M4Gy)
string substr = length.substr(length.length() – 1,1);

Just write down in plain language what that is supposed to mean??
I've been trying to figure out why this isn't working.

Could it be the 100+ errors and warnings that come up when try to compile.

If you actually typed this much code, you should be able to spot obvious mistakes like in line 65.
closed account (48T7M4Gy)
Could it be the 100+ errors and warnings that come up when try to compile.

As you probably know the (intimidating) 100 error list tends to collapse to just a few or none if you treat the first one then recompile rather than address them all at once.

We used to have races to see how many errors we could get - I think the best was about 2000, but the reality was they were more often than not solved by a typo or missing ; in one line, fortunately.
As you probably know the (intimidating) 100 error list tends to collapse to just a few or none if you treat the first one then recompile rather than address them all at once.

It was my feeble attempt at being facetious as I corrected two errors and it narrowed the list to 4, so yes I am aware, but why wouldn't have OP at least tried the same, because
I've been trying to figure out why this isn't working.
would suggest that it compiled, but not yielding the proper results.

There are a couple of problems with this post.

[A]
I don't have access to the question
So Dr50, you wrote 141 lines of code without any idea what the objective is.

[B] All lines at left margin is usually a sign that code was snagged from a webpage, which would suggest, was the assignment to come up with something/anything and when it wouldn't work, try to get somebody else to fix it.

Just a thought.
closed account (48T7M4Gy)
All lines at left margin is usually a sign that code was snagged from a webpage

On a personal level LearntSomethingNew++ seems to be the way to describe this situation. Cheers
Topic archived. No new replies allowed.