-2

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;
}
JaMiT
  • 14,422
  • 4
  • 15
  • 31
Sweep
  • 15
  • 2
  • PS: thanks for your time :) – Sweep Jun 15 '23 at 18:41
  • 2
    Add the exact error message. – HolyBlackCat Jun 15 '23 at 18:42
  • _"... it and it says it was not declared..."_ just a little vague. – Richard Critten Jun 15 '23 at 18:42
  • 4
    You are using VLAs in C++. VLAs are not Standard ISO C++. Write proper C++ and use a `std::vector`instead. Arrays are so ... cumbersome and error-prone. – Thomas Weller Jun 15 '23 at 18:42
  • Exactly **what** was not declared in the scope? – Programmer-RZ Jun 15 '23 at 18:44
  • 1
    Here you go (with 1 don't do this change) - live - https://godbolt.org/z/18GehP4cz - Variable Length Arrays `string cars[amount];` are not part of C++. – Richard Critten Jun 15 '23 at 18:44
  • 1
    And it's `std::cout`, not `cout`, `std::string`, not `string`. – Thomas Weller Jun 15 '23 at 18:45
  • 3
    `'\n';` on a line by itself does absolutely nothing. – tadman Jun 15 '23 at 18:45
  • 1
    The error should be as text, not screenshots. And the first lines are way more important than the last lines. – HolyBlackCat Jun 15 '23 at 18:46
  • I pasted the error message – Sweep Jun 15 '23 at 18:46
  • Please post code, errors, sample data or textual output here as plain-text, not as images that can be hard to read, can’t be copy-pasted to help test code or use in answers, and are barrier to those who depend on screen readers or translation tools. You can edit your question to add the code in the body of your question. For easy formatting use the `{}` button to mark blocks of code, or indent with four spaces for the same effect. The contents of a **screenshot can’t be searched, run as code, or easily copied and edited to create a solution.** – tadman Jun 15 '23 at 18:46
  • Are those the **first** error messages? When I copied-pasted-compiled the code, I got three other errors before the two in your question. You should always work from the first errors to the later ones, because sometimes later error messages are just the result of the compiler being confused by earlier errors. For example, the last two error messages I got (i.e. the two you are asking about) are purely artifacts of the third error (which is not mentioned in your question). – JaMiT Jun 16 '23 at 06:57

2 Answers2

1

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.

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
  • I added the vector container, but it still says project2.cpp:11:21: error: 'cars' was not declared in this scope 11 | vector cars(amount); – Sweep Jun 15 '23 at 18:59
  • #include #include #include #include #include #include void display(){ int amount; cout << "Car amount: "; cin >> amount; vector cars(amount); for(int i=0; i> cars[i]; } for(int i=0; i – Sweep Jun 15 '23 at 19:00
  • @Sweep Most of headers you included are redundant. You need to include headers #include #include include . And you need to use qualified names like std::vector> cars; And remove the using directive. – Vlad from Moscow Jun 15 '23 at 19:03
  • 1
    Note that a nice compromise between `using namespace std;` and `std::cin` everywhere, is to put `using std::cin; using std::cout;` at the top of the function that uses them repeatedly. – Ben Voigt Jun 15 '23 at 19:05
  • Yeah I know I just didn't delete the ones before and about the std::court and everything else, I don't think I need to use it since Im not really making huge scrips since Im basically a beginner. Using namespace std is just faster and more simple for me and it works great for now at my skill level. – Sweep Jun 15 '23 at 19:10
  • @Sweep Well, as I already wrote you need to place the using directive before the function definition where names from the namespace std are used. – Vlad from Moscow Jun 15 '23 at 19:12
1
//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;
}
Sc0lapasta
  • 11
  • 3
  • i'm sorry if i didn't respond in the way you intented, i used my knowing to try to give you the best response from me. i am also new in stack overflow, this is literally my first try of helping someone coding :) if the code i sent you is not working (it works on my w11 machine), please tell me so i can correct it, also i can inform myself on how to receive in input the lenght of an array if that's what you need. sorry for my bad english and i hope i helped you. – Sc0lapasta Jun 15 '23 at 19:10
  • It's perfectly fine, honestly this is the best response I have gotten thank you so much! – Sweep Jun 15 '23 at 19:32
  • 3
    `#define amount 5 `-> should be a `const int AMOUNT = 5`, avoid using unnecessary macros – underloaded_operator Jun 15 '23 at 19:33