0

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";
  • 1
    Stdout is line buffered by default. You need to add a newline. – Qix - MONICA WAS MISTREATED Feb 27 '22 at 15:52
  • 1
    How are you running your program? From a terminal? From inside an IDE? – Some programmer dude Feb 27 '22 at 15:54
  • 1
    What does the debugger reveal? Does the execution reach the line? – eerorika Feb 27 '22 at 15:54
  • Also please take some time to read [the help pages](http://stackoverflow.com/help), take the SO [tour], read [ask], as well as [this question checklist](https://codeblog.jonskeet.uk/2012/11/24/stack-overflow-question-checklist/). And please learn how to [edit] your questions to improve them, like showing us the input you give, the expected and actual (if any) output. – Some programmer dude Feb 27 '22 at 15:54
  • As a very probably hint about your problem (and which a quick session in a debugger would verify), think about `cin >> allat[N];` Where are you actually writing the input in your vector? – Some programmer dude Feb 27 '22 at 15:55
  • 5
    Discuss `cin >> allat[N];` with your [Rubber Duck](https://en.wikipedia.org/wiki/Rubber_duck_debugging). Focus on the value of `N`. – user4581301 Feb 27 '22 at 15:56
  • 1
    @Qix-MONICAWASMISTREATED "*Stdout is line buffered by default. You need to add a newline*" - except at program exit, when the buffer gets flushed automatically. – Remy Lebeau Feb 27 '22 at 19:30

1 Answers1

3

I think the problem is in the following lines:

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

..as these invoke Undefined Behaviour. 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.

user4581301
  • 33,082
  • 7
  • 33
  • 54
The Coding Fox
  • 1,488
  • 1
  • 4
  • 18