I've been writing a code for a uni assignment that includes a main menu (to go by each different action the user can do), and i've been having a problem with one the functions-the function is supposed to output all of the whole numbers from 2 to 1000 that can be divided by the sum of their own digits(i.e:24 can be divided by 6 which is 2+4 etc...). When the function is finished it's supposed to take the user back to the main menu but instead it just ends the program. I thought it might be returning 0 for some reason (defined to exit the program) but couldn't find it. Any help appreciated!
#include <iostream>
using namespace std;
int main()
{
int choice;
cout << "choose a function:\n"
"0.Exit\n"
"1.The sum of 1/n^2\n"
"2.Print numbers divided by sum of digits\n" //this is the one/
"3.Print arithmetic ot geometric seris\n"
"4.Tylor series\n";//3,4 haven't been written yet//
cin >> choice;
if (choice == 0)
{
return 0;
}
else if (choice == 1)
{
int number; //this is the varient the user types in and the varient representing the growing denominator//
float sum = 0; //this is the sum of the calculation outputed//
float i;
cout << "Please write a number to calculate:" << endl;
cin >> number;
for (i = 1; i <= number; i++)
{
sum = sum + (1 / (i * i));
}
cout << "the result of the calculation is:" << sum << endl;
return main();
}
else if (choice == 2)
{
int x = 2, y = 1000;
int sum = 0;
cout << "press x to start calculating" << endl;
cin >> x;
cout << "the numbers are:" << endl;
for (x = 2; x <= y; x++)
{
int firstDigit, secondDigit, thirdDigit, fourthDigit; //firstDigit defined from right to left(ones to tens,tens to hundreds etc...)
sum = sum + (x / x % 100 + x % 10);
#include <iostream>
usingnamespace std;
int main()
{
int choice;
cout << "choose a function:\n""0.Exit\n""1.The sum of 1/n^2\n""2.Print numbers divided by sum of digits\n" //this is the one/
"3.Print arithmetic ot geometric seris\n""4.Tylor series\n"; //3,4 haven't been written yet//
cin >> choice;
if (choice == 0) {
return 0;
} elseif (choice == 1) {
int number; //this is the varient the user types in and the varient representing the growing denominator//
float sum = 0; //this is the sum of the calculation outputed//
float i;
cout << "Please write a number to calculate:" << endl;
cin >> number;
for (i = 1; i <= number; i++) {
sum = sum + (1 / (i * i));
}
cout << "the result of the calculation is:" << sum << endl;
return main();
}
elseif (choice == 2)
{
int x = 2, y = 1000;
int sum = 0;
cout << "press x to start calculating" << endl;
cin >> x;
cout << "the numbers are:" << endl;
for (x = 2; x <= y; x++) {
int firstDigit, secondDigit, thirdDigit, fourthDigit; //firstDigit defined from right to left(ones to tens,tens to hundreds etc...)
sum = sum + (x / x % 100 + x % 10);
firstDigit = x % 10;
secondDigit = x / 10 % 10;
thirdDigit = x / 100 % 10;
fourthDigit = x / 1000 % 10;
if (x % (firstDigit + secondDigit + thirdDigit + fourthDigit) == 0) {
cout << x << endl;
}
}
return main();
}
}
> return main();
These are your problem!
For one thing, calling main() recursively is disallowed in C++.
For another, it's a sloppy way of implementing a while loop.
#include <iostream>
usingnamespace std;
int main()
{
int choice;
do {
cout << "choose a function:\n""0.Exit\n""1.The sum of 1/n^2\n""2.Print numbers divided by sum of digits\n" //this is the one/
"3.Print arithmetic ot geometric seris\n""4.Tylor series\n"; //3,4 haven't been written yet//
cin >> choice;
if (choice == 0) {
return 0;
} elseif (choice == 1) {
int number; //this is the varient the user types in and the varient representing the growing denominator//
float sum = 0; //this is the sum of the calculation outputed//
float i;
cout << "Please write a number to calculate:" << endl;
cin >> number;
for (i = 1; i <= number; i++) {
sum = sum + (1 / (i * i));
}
cout << "the result of the calculation is:" << sum << endl;
}
elseif (choice == 2)
{
int x = 2, y = 1000;
int sum = 0;
cout << "press x to start calculating" << endl;
cin >> x;
cout << "the numbers are:" << endl;
for (x = 2; x <= y; x++) {
int firstDigit, secondDigit, thirdDigit, fourthDigit; //firstDigit defined from right to left(ones to tens,tens to hundreds etc...)
sum = sum + (x / x % 100 + x % 10);
firstDigit = x % 10;
secondDigit = x / 10 % 10;
thirdDigit = x / 100 % 10;
fourthDigit = x / 1000 % 10;
if (x % (firstDigit + secondDigit + thirdDigit + fourthDigit) == 0) {
cout << x << endl;
}
}
}
} while ( choice != 0 );
}
Thank for the reply, I applied it and article 1 works well but the second now works in an endless loop...any idea why it wont stop? same code as above with the difference of the do while instead of return main()...
$ g++ -std=c++11 foo.cpp
$ ./a.out
choose a function:
0.Exit
1.The sum of 1/n^2
2.Print numbers divided by sum of digits
3.Print arithmetic ot geometric seris
4.Tylor series
2
press x to start calculating
2
the numbers are:
2
3
4
5
6
7
8
9
10
12
18
20
21
24
<< snipped >>
954
960
966
972
990
999
1000
choose a function:
0.Exit
1.The sum of 1/n^2
2.Print numbers divided by sum of digits
3.Print arithmetic ot geometric seris
4.Tylor series
But yeah, if you type in a literal 'x' at your prompt, then cin goes into an error state and you lose the game.