'cout' Does Not Appear In Console

Hi,

This is my first post! I have been working my way through the tutorials here at www.cplusplus.com, and I have been using what I learn in each stage to create a small program.

I originally created a program that let the user enter a radius and then calculated the circumference and area of the circle, however I lost the original program.

I recreated the program, but the problem is that not all of the output was 'outputed', if you understand. I tried this twice and only this portion of the output appears on the console:

Hello! Please enter the radius of any circle: 5
The circumference of your circle is: 31.4


However, what I desire to be on the console is this:

Hello! Please enter the radius of any circle: 5
You entered a radius of: 5

The area of your circle is: 78.5
the circumference of your circle is: 31.4


**Please note, the user inputs the radius, I put in '5' as an example**

Below is the code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#include <iostream>
using namespace std;

#define PI 3.14

int main ()
{
    double radius;
    double area;
    double circumference;

    cout << "Hello! Please enter the radius of any circle: ";
    cin >> radius;
    cout << "\r";
    cout << "You entered a radius of: ";
    cout << radius;

    area = PI * radius * radius;
    circumference = 2 * PI * radius;

    cout << "\r \r";
    cout << "The area of your circle is: ";
    cout << radius;
    cout << "\r";
    cout << "The circumference of your circle is: ";
    cout << circumference;

    return 0;
}


Thank you very, very much for reading and hopefully we can solve the problem! I'm sure I just did something stupid...

I am running windows 7 32bit and the compiler is Code::Blocks.

Thanks,
Top Hat
I've never seen the '/r' escape sequence but from google'ing it causes a carriage return? Not sure what that is. I would use '/n' or 'endl' to skip lines. Also, are you getting any compiler errors?
cout << "The area of your circle is: ";
cout << radius; //????????supposed to be area.

Use \n for a "newline" instead of \r.
\r is overwriting your next line.
Carriage return causes the cursor to go to the beginning of the same line (NOT the next line).

This will be better:
cout << "Hello! Please enter the radius of any circle: ";
cin >> radius;
cout << "\n";
cout << "You entered a radius of: " << radius << "\n"; //much more Readable.


area = PI * radius * radius;
circumference = 2 * PI * radius;


cout << "\n\nThe area of your circle is: ";
cout << area;
cout << "\n";
cout << "The circumference of your circle is: " << circumference << "\n";

Last edited on
don't use \r, use std::endl as nano511 said.

nano511,
on windows a new line is a combination of \r\n unlike unix where its only \n.
as others have posted dont use \r. also your variable was switched with the wrong one which is I also corrected(line 22 had radius instead of area). bottom compiles and gives the result you would expect. I just used endl instead of \n. either would have worked.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#include <iostream>
using namespace std;
#define PI 3.14

int main ()
{
    double radius;
    double area;
    double circumference;

    cout << "Hello! Please enter the radius of any circle: ";
    cin >> radius;
    cout << endl;
    cout << "You entered a radius of: ";
    cout << radius;

    area = PI * radius * radius;
    circumference = 2 * PI * radius;

    cout << endl << endl;
    cout << "The area of your circle is: ";
    cout << area;
    cout << endl;
    cout << "The circumference of your circle is: ";
    cout << circumference;
    cout << endl;

    return 0;
}
Last edited on
Thanks you so much everyone, you guys are awesome! :)

The problem seemed to be the "\r" because after I changed it to 'endl' everything worked as expected. I'm not sure where I picked up "\r" from but I'll be sticking to 'endl' now on. Is there any difference between 'endl' and "\n"?

Also, the next step was that I wanted to create a loop, so that after it gives you the measurements it asks you for a new radius... Would I use a while loop (ie. while the radius input is greater then 0, keep asking for a new radius)? If so, how would I go about that?

Thanks again,
Top Hat
make a infite loop, when you input radius, see if its 0 or less and break from there.

1
2
3
4
5
while(1)
{


}
There is not much difference between endl and \n.
endl, flushes the buffer while \n only goes to a new line.
Thanks guys, I did a 'do while' loop and it works a treat :)
@nano511

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
int counter = 1;

    while(counter != 100){	//A Fake "Loading...".

        cout << "Cracked: " << counter << "%\r";	//The '\r' is called "Carriage Return". It moves the cursor to the beginning of the same line and Hence OVERWRITES it.
        ping('1');

        if (!(counter % 7) || !(counter % 11))  //A "Dramatic" Delay, when the 'counter' is completely divisible by 7 or 11 (i.e. 7, 11, 14, 21, 22.....).
            ping('2');

        if (counter == 99)	//"Waste" a little more time when the number is '99' (maybe the program got Jammed).
            ping('5');

        counter++;
    }


The ping func.:
1
2
3
4
5
6
7
8
9
10
void ping (char time){
    string s1, s2, final_s;

    s1 = "@ping 1.11.11 -n ";
    s2 = " >null";
    final_s = s1 + time + s2;	//Joining the strings...

    system (final_s.c_str());	//...and using the member function c_str(), since 'system()' requires a const char* as an argument.
    system("@DEL null");
}


This is the main part of a program (for beginners) which can be found at http://dams.x10.mx/dams/Password_Cracker.html
put cout.flush()
after cout << "\r"

for example


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>

using namespace std ;

int main()
{

    for( int i = 0 ; i < 20000 ; i ++ )
    {
        if( i % 100 == 0 ){
            cout << "\r" << i ;
            cout.flush() ;
            for( int j = 0 ; j < 5000000 ; j++) ;  // for delay
        }
    }
    cout << endl ;

    return 0 ;
}
Last edited on
@obiwang

for( int j = 0 ; j < 5000000 ; j++) ; // for delay


This won't give you a precise "delay/lag" and will require some experimenting to change the delay(it even requires more work on the part of the processor-I have kept this statement in brackets because this program is small and it won't matter much.).

A better option will be to use sleep() and the appropriate headers.
Topic archived. No new replies allowed.