0

Hello guys this is my first time using stackoverflow and hope that you would oblige me.

so I was learning coding from codecademy and I was stuck in this project

Question- Write a program to find the sum of even numbers and the product of odd numbers in a vector.

//my code
#include <iostream>
#include <vector>

int main()
{
    std::cout << " This program is written to calculate the sum of even numbers and product of odd numbers\n";
    std::cout << "When you are done then plz press f";
    std::string ans;
    int k;
    std::cin >> ans;
    while (ans != "f") {
        std::cout << "Plz enter your number:";
        std::cin >> k;
        std::vector<int> num;
        num.push_back(k);
    }
    if (ans == "f") {
        std::cout << "Thanks for trying this program\n"
                  << "your Answers are\n";
        for (int i = 0; i <= num.size(); i++)
            if (num[i] % 2 == 0) {
                int even = even + num[i];
            }
            else {
                int odd = odd + num[i];
            }
    }
    std::cout << "Sum of even numbers is " << even << "\n";
    std::cout << "Sum of odd numbrs is " << odd << "\n";
}

error being shown-

review.cpp: In function ‘int main()’:
review.cpp:18:26: error: ‘num’ was not declared in this scope
     for (int i = 0; i <= num.size() ;i++)
                          ^~~
review.cpp:18:26: note: suggested alternative: ‘enum’
     for (int i = 0; i <= num.size() ;i++)
                          ^~~
                          enum
review.cpp:26:47: error: ‘even’ was not declared in this scope
     std::cout << "Sum of even numbers is " << even << "\n";
                                               ^~~~
review.cpp:27:45: error: ‘odd’ was not declared in this scope
     std::cout << "Sum of odd numbrs is " << odd << "\n";
                                             ^~~
review.cpp:27:45: note: suggested alternative: ‘void’
     std::cout << "Sum of odd numbrs is " << odd << "\n";
                                             ^~~
                                             void
drescherjm
  • 10,365
  • 5
  • 44
  • 64
  • 3
    *I was learning coding from codecademy* -- You should learn C++ from reading peer-reviewed C++ books, not websites. The fact that you didn't know why the error existed is a sign that the website you are going to is not geared towards actually teaching C++. Instead, the questions asked by that site (and other "online coding" sites) assume you know the language you will be using to answer the questions well enough to never make simple mistakes. – PaulMcKenzie Oct 22 '21 at 14:14
  • @PaulMcKenzie Thanks mate, can you plz recommend me a peer reviewed book. I tried searching google for books and also read the stackoverflow answer about the books but got more confused and thus decided to follow a guided path(i.e. through a coding website). I hope you wouldn't mind it. – Pranav Aggarwal Oct 22 '21 at 14:23
  • @PranavAggarwal [The Definitive C++ Book Guide and List](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) – bolov Oct 22 '21 at 14:28

1 Answers1

2

You cannot access variables outside of their scope. Thats what the error says. Your code reduced to the bare minimum is:

 int main() {
     bool condition = true;
     if (condition) {
          int x = 0;
     }
     x = 42;                  // error not declared in this scope
 }

And the same without the error:

 int main() {
     bool condition = true;
     int x = 0;
     if (condition) {
          x = 0;
     }
     x = 42;                 
 }
463035818_is_not_an_ai
  • 109,796
  • 11
  • 89
  • 185
  • mate can you please provide me with a link for reading about the error. I can't find a simplified one on google. – Pranav Aggarwal Oct 22 '21 at 14:26
  • @PranavAggarwal the variable **goes out of scope** in the first example – zoomlogo Oct 22 '21 at 14:52
  • In the first example, the lifetime of the variable `x` ends at the first `}` before the `x = 42;` In the second example the lifetime of `x` ends at the `}` at the end of `int main()` both are a result of the scope of the variable. – drescherjm Oct 22 '21 at 15:06
  • @drescherjm thanks man, got it! – Pranav Aggarwal Oct 22 '21 at 15:11
  • @PranavAggarwal If you find yourself not understand about some basic concepts or keywords of C++ you could search and check [cppreference](https://en.cppreference.com/), and read books. – rustyhu Oct 23 '21 at 05:12