| | |
Code Malfunction?
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Feb 2008
Posts: 3
Reputation:
Solved Threads: 0
I don't understand why this program is making me input two more times (after the program is finished) before it terminates. All other programs that I do usually terminates while this code idles. For those who are confused, I have to enter extra characters and hit "enter," twice before the program terminates.
Also, I don't understand this program:
I can't seem to figure out what cin.getline() means. The book that I'm learning from explains it very vague. And it doesn't do anything special. Should it? When I run the program, it terminates after I input something.
C++ Syntax (Toggle Plain Text)
#include <iostream> using namespace std; int main() { //I'm declaring some variables. int number1, number2, product, sum; cout << "Please enter an integar (whole number): \n"; cin >> number1; cout << "Please enter a another integar: \n"; cin >> number2; //Computing the numbers. product = number1 * number2; sum = number1 + number2; //This will reveal the final computations. cout << "The sum of " << number1 << " and " << number2 << " equals: " << sum << "\n"; cout << "The product of " << number1 << " and " << number2 << " equals: " << product << "\n"; cin >> sum >> product; return 0; }
Also, I don't understand this program:
C++ Syntax (Toggle Plain Text)
#include <iostream> using namespace std; int main() { char text[10]; cout << "Please enter a word: \n"; cin.getline(text, 10); cout << text << endl; return 0; }
I can't seem to figure out what cin.getline() means. The book that I'm learning from explains it very vague. And it doesn't do anything special. Should it? When I run the program, it terminates after I input something.
•
•
Join Date: Jul 2005
Posts: 1,688
Reputation:
Solved Threads: 266
In a typical program when all calculations are done and no further input is expected the program will terminate and the console screen will disappear. That's what happens in program #2. To have the program pause so you can see what's sent to the screen you can place a cin.get(); just before return 0; or you can do a cin >> x; where x is a valid variable. The latter is what's happening in program 2. You actually have 2 calls to >> just before the return 0; line so you need to enter two pieces of information before the program will close.
•
•
Join Date: Dec 2007
Posts: 226
Reputation:
Solved Threads: 1
I'll probably gain you some corrections to my post that will give you insight, but as a new guy I can tell you a couple things.
I'm not sure why you chose int for main, you could use void as you are only defining and outputting character strings
the first line, is defining a character array with 10 elements. In other words, a limit of 10 characters.
the next line outputs your plea for data to the screen.
The getline is used to enter a string of (usually) text. In yours, you're telling it to put that string into the text array...be advised you need to leave a space for the "null terminator" or you'll become very familiar with a problem deemed the "off by one" error
The next line outputs the text to the screen
The return line is what is causing your program to exit upon entry. A return code of 0 means the program did what it was supposed to do and everything worked right so it terminated. You should be able to take that out once you replace with
Others please post more details as you wish
I'm not sure why you chose int for main, you could use void as you are only defining and outputting character strings
the first line,
C++ Syntax (Toggle Plain Text)
char text [10]
the next line outputs your plea for data to the screen.
The getline is used to enter a string of (usually) text. In yours, you're telling it to put that string into the text array...be advised you need to leave a space for the "null terminator" or you'll become very familiar with a problem deemed the "off by one" error
The next line outputs the text to the screen
The return line is what is causing your program to exit upon entry. A return code of 0 means the program did what it was supposed to do and everything worked right so it terminated. You should be able to take that out once you replace
C++ Syntax (Toggle Plain Text)
int main ()
C++ Syntax (Toggle Plain Text)
void main ()
Others please post more details as you wish
In your program# 1,after computing sum and product and displaying them to th user , what's the use of
cin >> sum >> product;
This is again demanding that you enter two more values for sum and product.
Also , in your program # 2 , cin.getline is used to input a string as follows:
cin.getline(<destination string pointer>,<lenght of string to be entered>);
Also ,as henpecked1 advised you , you need to have one location extra in the string for the null ('\0') character that serves as end of string.
The see the output of a program you could include
#include<conio.h>
and use the function getch() just before return 0;
This will make the run time environment wait for the user to enter a character after displaying the results.
Using getche() will also display the character you entered
cin >> sum >> product;
This is again demanding that you enter two more values for sum and product.
Also , in your program # 2 , cin.getline is used to input a string as follows:
cin.getline(<destination string pointer>,<lenght of string to be entered>);
Also ,as henpecked1 advised you , you need to have one location extra in the string for the null ('\0') character that serves as end of string.
The see the output of a program you could include
#include<conio.h>
and use the function getch() just before return 0;
This will make the run time environment wait for the user to enter a character after displaying the results.
Using getche() will also display the character you entered
•
•
Join Date: Feb 2008
Posts: 3
Reputation:
Solved Threads: 0
first off, thanks for all the replies.
On the first program, the reason I added the cin >> sum >> product; was because, before I included that, the program would not calculate the numbers that I inputed. I followed the same code that the book had me do, but for some reason it didn't work. So, I worked with the things I knew and came up with adding the cin >> sum >> product; (along with some other adjustments). After this, the program now did the calculations but it also required me to input 2 more times. I'm reading the replies, but it seems like there isn't any clear thing to do...
The second program is confusing because when I enter in a word, the program just terminates. Is that the effect of adding cin.getline()? Because it doesn't seem like anything out of the ordinary.
Thanks for all the help so far.
On the first program, the reason I added the cin >> sum >> product; was because, before I included that, the program would not calculate the numbers that I inputed. I followed the same code that the book had me do, but for some reason it didn't work. So, I worked with the things I knew and came up with adding the cin >> sum >> product; (along with some other adjustments). After this, the program now did the calculations but it also required me to input 2 more times. I'm reading the replies, but it seems like there isn't any clear thing to do...
The second program is confusing because when I enter in a word, the program just terminates. Is that the effect of adding cin.getline()? Because it doesn't seem like anything out of the ordinary.
Thanks for all the help so far.
•
•
•
•
I'll probably gain you some corrections to my post that will give you insight, but as a new guy I can tell you a couple things.
I'm not sure why you chose int for main, you could use void as you are only defining and outputting character strings
int main() is a standard. Main function must return a value. Yesterday is a history, tomorrow is a mystery, today is a gift.
Behind every smile is a tear.
Visal .In
Behind every smile is a tear.
Visal .In
•
•
Join Date: Jul 2005
Posts: 1,688
Reputation:
Solved Threads: 266
>>Is that the effect of adding cin.getline()?
No. getline() has nothing to do with it. The console doesn't know you want to look at the output sent to the screen so as soon as it outputs the text variable to the screen it returns 0 to the operating system which causes the console screen to either go blank or disappear completely. If you want to view text after it's been output to the screen you have to tell the program to not do anything until you tell it to. You do that by having it wait indefinetly until it receives some input from a call to one of the istream methods (>>, get(), getch(), getche(), whatever----though some methods are preferred over others) or by using system("Pause"), which isn't a good choice; or by having the program do nothing useful, ie have it count to itself or sleep or whatever you want ot call it, for a given period of time until it moves on by calling the sleep() function (for example, if you want to have the output screen remain open for 10 seconds before it closes, but not stay open indefinetly waiting for input or until another system function is called).
No. getline() has nothing to do with it. The console doesn't know you want to look at the output sent to the screen so as soon as it outputs the text variable to the screen it returns 0 to the operating system which causes the console screen to either go blank or disappear completely. If you want to view text after it's been output to the screen you have to tell the program to not do anything until you tell it to. You do that by having it wait indefinetly until it receives some input from a call to one of the istream methods (>>, get(), getch(), getche(), whatever----though some methods are preferred over others) or by using system("Pause"), which isn't a good choice; or by having the program do nothing useful, ie have it count to itself or sleep or whatever you want ot call it, for a given period of time until it moves on by calling the sleep() function (for example, if you want to have the output screen remain open for 10 seconds before it closes, but not stay open indefinetly waiting for input or until another system function is called).
Last edited by Lerner; Mar 15th, 2008 at 6:48 pm.
![]() |
Similar Threads
- Need help for linked list (C++)
- Random No Signal Error (Troubleshooting Dead Machines)
- Spacing problems in Ie Gettting extra spacing between divs (HTML and CSS)
- help! a little malfunction in my program (C)
- From Page to Sitemap (HTML and CSS)
- wuts wrong with my code here? (C++)
Other Threads in the C++ Forum
- Previous Thread: Unresolved Externals
- Next Thread: please help me debugg this program (grading System)
| Thread Tools | Search this Thread |
api array arrays based beginner binary c++ c/c++ calculator char char* class classes code compile compiler console conversion count delete deploy desktop directshow dll download dynamic dynamiccharacterarray encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelp homeworkhelper iamthwee ifstream input int integer java lib linkedlist linker linux list loop looping loops map math matrix memory news number numbertoword output parameter pointer problem program programming project python random read recursion recursive reference return rpg sorting string strings struct temperature template templates test text text-file tree unix url variable vector video visual visualstudio win32 windows winsock wordfrequency wxwidgets






