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;
}
The problem is that the using directive is placed before main
where neither name from the namespace
stdis used. On the other hand, you are using names from the standard namespacestdwithin the functiondisplaythat is placed before the using directive.So names as for example
cout,cinandstringare 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
are not a standard C++ feature.
Instead use container
std::vector<std::string>likeTo use the container you need to include header
<vector>.Also this expression statement
has no effect. Remove it.
Instead you could write for example
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
and so on.