Understanding C++ Program Inputs: A Beginner's Guide

In summary, the conversation discusses the topic of input operations in C++, specifically the behavior of the cin command. The conversation includes a code example and the question of why the second cin command reads "there" after the first one reads "hello". The experts explain that this is the intended behavior of the language and allows for more flexibility in reading and processing input.
  • #1
RuthKom
2
0
I'm a beginner and currently learning programming by myself. when I read a book I came across an example which I don't quite understand.
Code:
#include <iostream>

using std::cout;
using std::cin;
using std::endl;

int main()
{
   char string1[ 20 ];                // reserves 20 characters
   char string2[] = "string literal"; // reserves 15 characters

   // read string from user into array string2
   cout << "Enter the string \"hello there\": ";
  [B] cin >> string1;                   // reads "hello" [/B]
   cout<< "\nstring1 is: " <<string1<<endl;

   [B]cin >> string1;  // reads "there"[/B]
   cout << "\nstring1 is: " << string1 << endl;

   return 0;  
}

I know that space terminates the input when the first "cin>>string1" statement is executed, but I don't know why the second "cin>>string1" reads "there"...can anyone tell me the reason behind?
Please help me...Thanks very much:)
 
Technology news on Phys.org
  • #2
The second input operation starts reading where the first one stopped. Or have I missed the point of your question?
 
  • #3
Thx jtbell
I want to know why it starts reading where the first one stopped...
 
  • #4
cin operates on the basis of a console or terminal. The terminal driver "sends" blocks of text to the OS -> program when it gets a carraige return - newline - ie., you hit <return>
 
  • #5
RuthKom said:
I want to know why it starts reading where the first one stopped...

Because that's the way the language was designed!

I think it's useful, because it let's you read part of a line, examine what you've read, and then read the rest of the line differently depending on what the first part of the line contains.
 

1. What is the purpose of understanding C++ program inputs?

Understanding C++ program inputs is crucial for writing efficient and error-free code. Inputs are the data or values that are provided to a program, and they can greatly affect the output of a program. By understanding how inputs are handled in a C++ program, you can ensure that your code is properly executed and produces the desired results.

2. How do I receive user inputs in a C++ program?

In C++, the cin object is used to receive user inputs from the standard input stream. This can be done using the >> operator, which reads the input and assigns it to a variable. For example, cin >> num; will prompt the user to enter a value, which will then be stored in the variable num.

3. How can I handle invalid inputs in my C++ program?

Invalid inputs can cause errors or unexpected behavior in a C++ program. To handle this, you can use input validation techniques such as checking the data type or range of the input, and prompting the user to re-enter a valid input if necessary. You can also use exception handling to catch and handle any errors that may occur.

4. What is the difference between command line inputs and user inputs in a C++ program?

Command line inputs are provided to a C++ program when it is executed from the command line or terminal. They are passed as arguments to the main() function and can be accessed using the argc and argv parameters. On the other hand, user inputs are provided during the execution of the program and are handled using the cin object.

5. Are there any best practices for handling inputs in a C++ program?

Yes, there are some best practices that can help you handle inputs effectively in your C++ program. These include validating inputs, handling errors and exceptions, and using appropriate data types for inputs. It is also important to properly document your code to make it easier for others to understand and use.

Similar threads

  • Programming and Computer Science
Replies
10
Views
2K
  • Programming and Computer Science
Replies
12
Views
1K
  • Programming and Computer Science
Replies
18
Views
2K
  • Programming and Computer Science
Replies
5
Views
4K
Replies
10
Views
957
  • Programming and Computer Science
Replies
5
Views
2K
  • Programming and Computer Science
Replies
8
Views
2K
  • Programming and Computer Science
Replies
2
Views
871
  • Programming and Computer Science
Replies
6
Views
8K
  • Programming and Computer Science
2
Replies
39
Views
3K
Back
Top