C++ "was not declared in this scope"

867 Views Asked by At

Heyo, basically I was writing this simple function to ask you how many cars you have, inputing the amount in the array, and assigning the names of the cars to the array aswell. Also made a for loop to make display it and it says it was not declared in the scope and I just can't figure it out why it says this. Can someone help me out?

error:           project2.cpp:13:16: error: 'cars' was not declared in this scope
   13 |         cin >> cars[i];
      |                ^~~~
project2.cpp:17:17: error: 'cars' was not declared in this scope
   17 |         cout << cars[i];
#include <iostream>
#include <string>
void display(){
    int amount;
    cout << "Car amount: ";
    cin >> amount;
    string cars[amount];
    for(int i=0; i<amount; i++){
        cout << "Enter " << i << " car name:";
        cin >> cars[i];
        '\n';
    }
    for(int i=0; i<amount; i++){
        cout << cars[i];
    }

}
using namespace std;
int main()
{
    
    display();


    return 0;
}
2

There are 2 best solutions below

6
Vlad from Moscow On

The problem is that the using directive is placed before main

using namespace std;
int main()
{
    
    display();


    return 0;
}

where neither name from the namespace std is used. On the other hand, you are using names from the standard namespace std within the function display that is placed before the using directive.

void display(){
    int amount;
    cout << "Car amount: ";
    cin >> amount;
    string cars[amount];
    for(int i=0; i<amount; i++){
        cout << "Enter " << i << " car name:";
        cin >> cars[i];
        '\n';
    }
    for(int i=0; i<amount; i++){
        cout << cars[i];
    }

}

So names as for example cout, cin and string are not found. At least you should place the using directive before the function definition.

Pay attention to that variable length arrays as the array used within the function

    string cars[amount];

are not a standard C++ feature.

Instead use container std::vector<std::string> like

std::vector<std::string> cars( amount );

To use the container you need to include header <vector>.

Also this expression statement

'\n';

has no effect. Remove it.

Instead you could write for example

for(int i=0; i<amount; i++){
    cout << "Enter " << i << " car name:";
    cin >> cars[i];
}

cout << '\n';

for(int i=0; i<amount; i++){
    cout << cars[i] << '\n';
}

In general it is a bad idea to use the using directive in the global namespace. It is much better to use qualified names like for example

    std::cout << "Car amount: ";

and so on.

3
Sc0lapasta On
//new code:
#include <iostream>
#include <string>
#define amount 5 //<---- defines the number of cars in the array
using namespace std;

void display() 
{
    //int amount;
    //cout << "Car amount: ";
    //cin >> amount;
    string cars[amount]; //now, you have a defined number of cars, c++ doesnt allow to use a variables as lenght of array

    for (int i = 0; i < amount; i++) 
    {
        cout << "Enter " << i + 1 << " car name:" << endl; //added endl instead of \n
        //                      ^more undestandable as it starts from 1 instead of 0
        cin >> cars[i];
        //'\n'; doesnt work like this, it need to be used in a string as single character, like "\ncars"
    }
    for (int i = 0; i < amount; i++) 
    {
        cout << cars[i] << " "; //more understandable
    }

}

int main()
{
    display();
    return 0;
}