Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

break statement in c++ by pressing enter

Tags:

c++

enter

break

#include <iostream>
using namespace std;
int main()
{
    int n,t=0,k=0;
    cin>>n;
    char data[n][100];
    int num[n];
    for(int i=0;i<n;i++)
{
    while(1)
    {
        cin>>data[i][t];
        cout<<data[i][t]<<endl;
        if(data[i][t]=='\n') break;
        k++;
        if(k%2==1) t++;
    }
    cout<<i;
    num[i]=(t-2)/2;
    k=0;
t=0;
 }

    for(int i=0;i<n;i++)
    {
        while(1)
        {
            cout<<data[i][t];
            if(t==num[i]) break;
            t++;
        }
        t=0;
    }
}

here is the code I have written in c++ which gives the even numbered characters from the starting half of every word given by the user but when giving input after pressing enter the loop should break but the loop is not breaking

while(1)
{
    cin>>data[i][t];
    cout<<data[i][t]<<endl;
    if(data[i][t]=='\n') break;
    k++;
    if(k%2==1) t++;
}
like image 983
Anurag Ramteke Avatar asked Jun 13 '16 19:06

Anurag Ramteke


People also ask

What is a break statement in C++?

When a break statement is encountered inside a loop, the loop is immediately terminated and the program control resumes at the next statement following the loop. It can be used to terminate a case in the switch statement (covered in the next chapter).

How do you break a loop in C++?

Break Statement in C/C++. Break Statement is a loop control statement which is used to terminate the loop. As soon as the break statement is encountered from within a loop, the loop iterations stops there and control returns from the loop immediately to the first statement after the loop. Syntax: break;

How to use break statement with IF condition in JavaScript?

So will use the break statement with the if condition which compares the key with array elements as shown below: Nested Loops: We can also use break statement while working with nested loops. If the break statement is used in the innermost loop. The control will come out only from the innermost loop.

How do you break a loop in a switch statement?

You must write break statement inside a loop or switch. Use of break keyword outside the switch or loop will result in compilation error. break statement is generally used with condition ( if statements) inside loop. In case of nested loop or switch, break only terminates the innermost loop or switch.


1 Answers

By default formatted input using the "input" operators >> skip white-space, and newline is a white-space character. So what's happening is that the >> operator simply waits for some non-white-space input to be entered.

To tell the input to not skip white-space you have to use the std::noskipws manipulator:

cin>>noskipws>>data[i][t];
like image 159
Some programmer dude Avatar answered Sep 24 '22 04:09

Some programmer dude