I have heard about the:
char myline[100];
cin.getline(myline,100);

but when my coding is long it doesnt work i gues as long as i would like it to?

here is my code for example:

#include <iostream>
using namespace std;
int main()
{
    int length;
    int width;

    cout << "Enter the length: ";
    cin >> length;
    cout << "Enter the width: ";
    cin >> width;

    cout << "The area is ";          <---- This is the problem, when it gets here it just closes...
    cout << length*width;

    char myline[100];             <----- Does it have to do with this? 
    cin.getline(myline,100);
    return 0;
}

Yes i am extremely new to programming and i am trying to learn it, but there is a problem.
When i enter the two variables from my keyboard, and it gets to the part where it should say "The area is " and show my answer, the program just shuts off... Does this have to do with the char myline? Someone please help.

Recommended Answers

All 3 Replies

The reason it doesn't work for you is because after entering an integer cin >> width the system leaves the '\n' in the keyboard buffer, and the next time cin is called it gets that '\n' instead of wanting for another keyboard input. What you have to do is flush the '\n' from the keyboard buffer after the integer input. See this thread by Narue to see how that is done.

char myline[100];             <----- Does it have to do with this? 
    cin.getline(myline,100);

you can delete that if you use Narue's suggestion.

Here is your code revised. Good luck.

#include <iostream>
using namespace std;
int main()
{
int length;
int width;

cout << "Enter the length: ";
cin >> length;
cout << "Enter the width: ";
cin >> width;
cout << "The area is ";
cout << length*width;
cout << "\n";
system("PAUSE");
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.