Home > front end >  Why does cout writing out nothing?
Why does cout writing out nothing?

Time:02-28

Im a begginer in c and i dont know why this code isnt running. Its writing out nothing... not even the "a"

#include <iostream>
#include <vector>
using namespace std;
int main()
{
    int N;
    cin >> N;
    vector<string> allat(N), etel(N);

    for(int a=0;a<N;a  ) {
        cin >> allat[N];
        cin >> etel[N];
    }

    int meg=0;

    for(int a=0;a<N;a  ) {
        for(int b=0;b<N;b  ) {
            if(etel[a] == allat[b]) {
                meg  ;
            }
        }
    }



    cout << meg << "a";

CodePudding user response:

I think the problem is in the following lines:

cin >> allat[N];
cin >> etel[N];

..as these throw exceptions. You should replace N with a, so the above can be replaced by:

cin >> allat[a];
cin >> etel[a];

Now your code should work. Also, look up to why is "using namespace std" considered as a bad practice.

CodePudding user response:

As pointed out by @Qix, add endl like this:

cout << meg << endl;

endl is preferred over \n because the latter is *nix specific newline escape character.

Also, drop the using namespace std; and practice adding std:: prefix for cout and other standard library functions.

Edit: endl is also part of std namespace.

  • Related