-1

I am writing a code in c++, its compiling perfectly but for some reason it's not moving beyond a particular line. I have checked and rechecked all the parenthesis. I am not able to figure out why this is happening as compiler is also not giving me any error or warning. I have not included the whole code, please let me know if more code is required.

int main()
{

    //declaration of all the required variable used below
for(int g=0;g<e;g++)
{
    //some code here
}
for(int i=0;i<e;i++)
{
       //some code here
}



for(int k=1;k<v;k++)
{
    //some code here
}


cout<<"\n";
cout<<"reaching here

for(int k=0;k<v;k++)
{
         //some code here
}
cout<<"\n";  //printing a next line 

     cout<<"not printing this line";
return 0;
 }
nitinsh99
  • 3,012
  • 3
  • 37
  • 73
  • What's `e` and `v`? They are not declared anywhere. – Igor Tandetnik Oct 21 '13 at 00:10
  • More code is *definitely* required. I have a feeling you are changing the value of k or v in the last loop or returning somehow. – Dhaivat Pandya Oct 21 '13 at 00:10
  • Did you use the debugger? – Jerry Oct 21 '13 at 00:10
  • @Jerry how do I do that, in this situation – nitinsh99 Oct 21 '13 at 00:13
  • The code you've posted doesn't compile let alone run. Obviously you have a problem somewhere between "main" and the line that says "not printing this line", but the problem will be in the code you did not post. I suggest you try to recreate the problem using something like http://www.ideone.com/ and then post the code that is able to recreate it, if you don't solve it yourself in getting to that point. – kfsone Oct 21 '13 at 00:14
  • Here's a link to the code you posted above on ideone.com: http://ideone.com/Bxst37 as you can see, it didn't compile. You should also go ahead and do `cout << "text for this line" << endl;` instead of leaving the lines hanging and/or using `"\n"` – kfsone Oct 21 '13 at 00:16
  • Here is another link, fixing the compile errors in the code you pasted, and adding the missing end-of-lines to the cout's, it works. http://ideone.com/e6VShc. So either it's printing but not being output or the problem is in the code you did not post. – kfsone Oct 21 '13 at 00:19
  • Whoever down voted this please know that this is my major project. I can't write the full code obviously it's too long. I had a feeling this has something to do with same variables used again or newline as @DhaivatPandya suggested. – nitinsh99 Oct 21 '13 at 00:19
  • @kfsone thanks that helped. Please write it as a solution for reps. – nitinsh99 Oct 21 '13 at 00:24
  • @nitinsh99 step 1: attach the debugger and determine where the program is failing. step 2: reduce the code from the original to the minimal [sscce](http://sscce.org/) that reproduces it. if you don't discover the cause in that process, you will have enough for us to help you. – kfsone Oct 21 '13 at 00:24

2 Answers2

1

It's possible the problem was the absence of end-of-lines after the lines you were printing. If this is not the case, you should be able to attach the debugger (I can explain how if you can respond to this answer with which environment you are using: Win, Lin, Mac) and determine where and when it crashes.

C++ iostreams provide "std::endl" for portable end-of-line, it's best not to mix it with "\n".

Using ideone.com I cleaned up the pasted code to compile and cleaned up the cout statements, and it appears to work:

int main()
{
    int e = 10;
    int v = 12;

    //declaration of all the required variable used below
    for(int g=0;g<e;g++)
    {
        //some code here
    }

    for(int i=0;i<e;i++)
    {
        //some code here
    }

    for(int k=1;k<v;k++)
    {
        //some code here
    }


    cout << endl << "reaching here" << endl;

    for(int k=0;k<v;k++)
    {
         //some code here
    }

    cout << endl << "not printing this line" << endl;

    return 0;
}

ideone link: http://ideone.com/e6VShc

kfsone
  • 21,566
  • 2
  • 35
  • 66
  • 1
    [this](http://stackoverflow.com/questions/1716296/why-does-printf-not-flush-after-the-call-unless-a-newline-is-in-the-format-strin) explains well why the newline is needed incase anyone is interested. – Stephan van den Heuvel Oct 21 '13 at 00:30
0

I think it is printing it but you did not see it because it is in the same line where you write commands.

user1061392
  • 286
  • 3
  • 13