-
Return not working?
I really hate to ask this but could someone look this over and see if I did something wrong as far as why the functions do not return right?
Code:
#include <cstdlib>
#include <iostream>
#include <iomanip>
#include <cstring>
#include <ctime>
using namespace std;
void random(); void multiples(); void numbers();
//------------------------------------------
int main() {
//------------------------------------------
cout<< "Pick a program to do. Enter 0 to quit.\n";
cout<< "Choice 1 - random selection\nChoice 2 - multiples\nChoice 3 - Number counting\n";
int choice=cin.get();
do{
switch (choice) {
case '1': random(); break;
case '2': multiples(); break;
case '3': numbers(); break;
default: cout<< "Try again ";
}
}while (choice != 0);
return 0;
}
//------------------------------------------
void random() {
//------------------------------------------
int score[101];
int frequency[101];
int upick=1;
while (upick!=0) {
//copy 0 into the first 101 bytes of score and frequency
memset(score, 0, 101*sizeof(int));
memset(frequency, 0, 101*sizeof(int));
cout<< "Pick a number between 1 and 100 and I will randomly select them and "<< endl
<< "then I will output the results.\n\nPick a number between 1 and 100: ";
cin>> upick; cout<< endl;
srand(time(0));
// Score entering loop
int entered=0, l=0;
for (int i=0; i<101; i++) {
score[i]=1+rand()%upick;
entered++;
if (score[i]>0){
cout << score[i]<< "\t"; ++l;
}
if (l==9) {
l=0; cout<< "\n";
}
}
cout<< "\n\n"; system("PAUSE");
// Score counting loop
for (int i=0; i < entered; i++)
++frequency[score[i]];
cout << "SCORE" << setw(13) << "FREQUENCY" << setw(8)<< "GRAPH"<< endl;
// Score output loop
for (int i=0; i < 101; i++) {
if (frequency[i]>0) {
cout<< setw(7) << i << setw(8) << frequency[i]<< setw(6)<< " ";
// Print graph
for(int j=0; j < frequency[i]; j++) {
cout << '*';
}
cout << endl;
}
}
cout<<"QUIT? Enter 0=yes 1=no: "; cin>> upick;
}
return;
}
//------------------------------------------
void multiples() {
//------------------------------------------
cout<<"Enter in a number between 1 and 12100 and I will calculate the multiples of\nthat number up to 86. Press -1 to quit.\n";
for (int y; y<12100;) {
cout<<"\n0 to quit. Your number: "; cin>> y;
while (y>12100) {cout<<"too big\n"; cin>>y;}
if (y<1) break;
int z=0; cout<< "\n";
for (int x=1; x<86; ++x) {
if (z>=4) {cout<< "\n"; z=0;}
cout << x<< "*"<< y<< "= " << x*y << setw(5);
++z;
}
y=0; cout<< endl;
}
return;
}
//------------------------------------------
void numbers() {
//------------------------------------------
int score[1000001];
int frequency[1000001];
int temp=1;
while (temp!=0) {
//copy 0 into the first 1000001 bytes of score and frequency
memset(score, 0, 1000001*sizeof(int));
memset(frequency, 0, 1000001*sizeof(int));
cout << "This program will allow you to enter in 1 million numbers up to a value "<< endl
<< "of 1 million and it will chart how many times you entered in that number."<< endl
<< "\nEnter scores (Limit: 1000000):\n";
// Score entering loop
int entered=0;
for (int i=0; i < 1000001; i++) {
cout << "Enter 0 when finished. Next score: ";
cin >> temp;
while(temp > 1000000) {
cout << "Score out of range. Enter again: ";
cin >> temp;
}
if (temp<1)
break;
score[i] = temp;
entered++;
}
// Score counting loop
for (int i=0; i < entered; i++)
++frequency [score[i]];
cout << "\nSCORE" << setw(13) << "FREQUENCY" << setw(8)<< "GRAPH"<< endl;
// Score output loop
for (int i=0; i < 1000001; i++) {
if (frequency[i]>0) {
cout<< setw(7) << i << setw(8) << frequency[i]<< setw(6)<< " ";
// Print graph
for(int j=0; j < frequency[i]; j++) {
cout << '*';
}
cout << endl;
}
}
cout<< "QUIT? 0=yes 1=no "; cin>> temp;
}
return;
}
-
Aren't your header files supposed to have an h at the end like this:
#include <blah.h>
I don't know though because I am like totally new to C++
-
No they aren't.
Again you have the score and frequency arrays inside a function. This function will crash when called.
-
I thought that was only for main() but I did fix it. It still does the same exact thing.
Here is what someone else said:
You set the functions to void, therefore u cannot return anything unless you have a pointer.
So I also changed the functions to int and had to remove return alltogether to even get it to compile. But it still does not return.
-
No, no. You can't return a value. That means you can't write
return 5;
But
return;
doesn't return a value, it simply returns control to the caller - that is, it exits the function.
return;
directly followed by the closing brace (}) of the function is redundant.
What do you mean they don't return?
The problem is that you don't give the user a chance to give a different command after one function executed. That means that the
do...while() loop in main is infinite once entered because choice doesn't change.
-
The stack limit applies for each and every function.
-
Finished!! Thank you cornedbee. I learned something.
Code:
#include <cstdlib>
#include <iostream>
#include <iomanip>
#include <cstring>
#include <ctime>
using namespace std;
void random(); void multiples(); void numbers();
//------------------------------------------
int main() {
//------------------------------------------
char choice;
do{
cout<< "\nPick a program to do. Enter 0 to quit.\n";
cout<< "Choice 1 - random selection\nChoice 2 - multiples\nChoice 3 - Number counting\n";
cin>> choice;
switch (choice) {
case '1': random(); break;
case '2': multiples(); break;
case '3': numbers(); break;
default: cout<< "Try again ";
}
}while (choice != '0');
return 0;
}
int scoreR[101];
int frequencyR[101];
//------------------------------------------
void random() {
//------------------------------------------
int upick=1;
while (upick!=0) {
//copy 0 into the first 101 bytes of scoreR and frequencyR
memset(scoreR, 0, 101*sizeof(int));
memset(frequencyR, 0, 101*sizeof(int));
cout<< "Pick a number between 1 and 100 and I will randomly select them and "<< endl
<< "then I will output the results.\n\nPick a number between 1 and 100: ";
cin>> upick; cout<< endl;
srand(time(0));
// Score entering loop
int entered=0, l=0;
for (int i=0; i<101; i++) {
scoreR[i]=1+rand()%upick;
entered++;
if (scoreR[i]>0){
cout << scoreR[i]<< "\t"; ++l;
}
if (l==9) {
l=0; cout<< "\n";
}
}
cout<< "\n\n"; system("PAUSE");
// Score counting loop
for (int i=0; i < entered; i++)
++frequencyR[scoreR[i]];
cout << "SCORE" << setw(13) << "FREQUENCY" << setw(8)<< "GRAPH"<< endl;
// Score output loop
for (int i=0; i < 101; i++) {
if (frequencyR[i]>0) {
cout<< setw(7) << i << setw(8) << frequencyR[i]<< setw(6)<< " ";
// Print graph
for(int j=0; j < frequencyR[i]; j++) {
cout << '*';
}
cout << endl;
}
}
cout<<"QUIT? Enter 0=yes 1=no: "; cin>> upick;
}
}
//------------------------------------------
void multiples() {
//------------------------------------------
cout<<"Enter in a number between 1 and 12100 and I will calculate the multiples of\nthat number up to 86. Press -1 to quit.\n";
for (int y=0; y<12100;) {
cout<<"\n0 to quit. Your number: ";
cin>> y;
while (y>12100) {
cout<<"too big\n"; cin>>y;
}
if (y<1) break;
int z=0; cout<< "\n";
for (int x=1; x<86; ++x) {
if (z>=4) {cout<< "\n"; z=0;}
cout << x<< "*"<< y<< "= " << x*y << setw(5);
++z;
}
y=0; cout<< endl;
}
}
int scoreN[1000001];
int frequencyN[1000001];
//------------------------------------------
void numbers() {
//------------------------------------------
int temp=1;
while (temp!=0) {
//copy 0 into the first 1000001 bytes of scoreN and frequencyN
memset(scoreN, 0, 1000001*sizeof(int));
memset(frequencyN, 0, 1000001*sizeof(int));
cout << "This program will allow you to enter in 1 million numbers up to a value "<< endl
<< "of 1 million and it will chart how many times you entered in that number."<< endl
<< "\nEnter scores (Limit: 1000000):\n";
// Score entering loop
int entered=0;
for (int i=0; i < 1000001; i++) {
cout << "Enter 0 when finished. Next score: ";
cin >> temp;
while(temp > 1000000) {
cout << "Score out of range. Enter again: ";
cin >> temp;
}
if (temp<1)
break;
scoreN[i] = temp;
entered++;
}
// Score counting loop
for (int i=0; i < entered; i++)
++frequencyN [scoreN[i]];
cout << "\nSCORE" << setw(13) << "FREQUENCY" << setw(8)<< "GRAPH"<< endl;
// Score output loop
for (int i=0; i < 1000001; i++) {
if (frequencyN[i]>0) {
cout<< setw(7) << i << setw(8) << frequencyN[i]<< setw(6)<< " ";
// Print graph
for(int j=0; j < frequencyN[i]; j++) {
cout << '*';
}
cout << endl;
}
}
cout<< "QUIT? 0=yes 1=no "; cin>> temp;
}
}