Hi all, how come my vector isn't displaying the previous results? Thanks.
[code]
#include <iostream>
#include <vector>
using namespace std;
//This function will subtract 2 integers and return the result.
int subtract(){
int num1;
int num2;
cout << "Enter 2 integers to subtract: ";
cin >> num1;
cin >> num2;
int result = num1 - num2;
return result;
}
//This function will add 2 integers and return the result.
int add(){
int num1;
int num2;
cout << "Enter 2 integers to add: ";
cin >> num1;
cin >> num2;
int result = num1 + num2;
return result;
}
//This funtion will display the menu.
int menu(){
int num;
cout << "[1] Add two numbers. " << endl;
cout << "[2] Subtract two numbers. " << endl;
cout << "[3] Display previous results." << endl;
cout << "[4] EXIT" << endl;
cout << "PLEASE ENTER A NUMBER TO EXECUTE: " << endl;
cin >> num;
return num;
}
//This function will ask user if they want to choose again.
int menuOrBye(){
cout << "Would you like to choose again?: ";
char choice;
cin >> choice;
return choice;
}
//This is the main function.
int main(){
vector<int> vect;
// int variables are declared/initialized.
int choice;
int result;
int num = menu();
//This will be executed if option 1 is selected.
if(num == 1){
result = add();
cout << "The answer is: " << result << endl;
vect.push_back(result);
choice = menuOrBye();
if(choice == 'Y'){
main();
}
else{
cout << "GOODBYE!";
}
}
//This will be executed if option 2 is selected.
else if(num == 2){
result = subtract();
cout << "The answer is: " << result;
vect.push_back(result);
choice = menuOrBye();
if(choice == 'Y'){
main();
}
else{
cout << "GOODBYE!";
}
}
//Executed if option 3 is selected.
else if(num == 3){
cout << "The previous results are: " << endl;
for(int i = 0; i < vect.size(); i++){
cout << vect.at(i);
}
}
//This will be executed if option 4 is selected.
else if(num == 4){
cout << "GOODBYE!";
}
#include <iostream>
#include <vector>
usingnamespace std;
//This function will subtract 2 integers and return the result.
int subtract(){
int num1;
int num2;
cout << "Enter 2 integers to subtract: ";
cin >> num1;
cin >> num2;
int result = num1 - num2;
return result;
}
//This function will add 2 integers and return the result.
int add(){
int num1;
int num2;
cout << "Enter 2 integers to add: ";
cin >> num1;
cin >> num2;
int result = num1 + num2;
return result;
}
//This funtion will display the menu.
int menu(){
int num;
cout << "[1] Add two numbers. " << endl;
cout << "[2] Subtract two numbers. " << endl;
cout << "[3] Display previous results." << endl;
cout << "[4] EXIT" << endl;
cout << "PLEASE ENTER A NUMBER TO EXECUTE: " << endl;
cin >> num;
return num;
}
//This function will ask user if they want to choose again.
int menuOrBye(){
cout << "Would you like to choose again?: ";
char choice;
cin >> choice;
return choice;
}
//This is the main function.
int main(){
vector<int> vect;
// int variables are declared/initialized.
int choice;
int result;
int num = menu();
//This will be executed if option 1 is selected.
if(num == 1){
result = add();
cout << "The answer is: " << result << endl;
vect.push_back(result);
choice = menuOrBye();
if(choice == 'Y'){
main();
}
else{
cout << "GOODBYE!";
}
}
//This will be executed if option 2 is selected.
elseif(num == 2){
result = subtract();
cout << "The answer is: " << result;
vect.push_back(result);
choice = menuOrBye();
if(choice == 'Y'){
main();
}
else{
cout << "GOODBYE!";
}
}
//Executed if option 3 is selected.
elseif(num == 3){
cout << "The previous results are: " << endl;
for(int i = 0; i < vect.size(); i++){
cout << vect.at(i);
}
}
//This will be executed if option 4 is selected.
elseif(num == 4){
cout << "GOODBYE!";
}
return 0;
}
The reason your code is not working as expected is because when you call main() manually, you are actually calling main with an entirely new stack frame. Every time you call a function, its local variables are allocated on a stack frame and are freed once the scope ends. When you call main() manually, you are creating a new stack frame with new local variables, which means vect is empty when you call "at" on it.
In theory, you could fix this by using static variables, but I would just recommend restructuring your code entirely.