简体   繁体   中英

how to exit after no input in a c++ program+

Hi guys i am new to c++, I just wrote this code to find min/max of a array of numbers.
I just want to know how can I make the no. of entries flexible(I mean the user should be able to enter as many entries as possible without specifying how many in the starting)
Here's the code, but its not working, can someone please help? Thanks code:

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

    cout<<"Program to calculate max/min/second max\n";
    int *A;
    A=new int[5];
    bool flag=false;
    int x=0,i=0;
    cout<<"Enter the numbers\n";

    do{

        cin>>x;
        if(x=='\0'){
            flag=true;
        }
        *(A+i)=x;
        i++;

    }
    while(!flag);

    for(int j=0;j<i;j++){


        cout<<*(A+j)<<"\n";

    }

    return 0;
}

You are confused that pressing enter will just give you the null terminator. It will not. Depending on your platform it will give you a carriage return/line-feed ( \\r\\n in Win, \\n in *ix). The best way to do this is to just have them use a letter like 'q' for quit or a number like -1, and then compare on that.

Dynamic Memory Allocation is a bit of a tricky subject for a beginning programmer (in C and C++ in any case.) The easiest way is to have the user specify how many entries, but you don't want this.

Otherwise using the vector class over an array is probably a better (and easier to grapple with than directly using pointers.)

http://www.cplusplus.com/reference/stl/vector/ is a good place to start. Look at the syntax for creating the vector and the push_back() member function to accomplish your task.

Good Luck,

SegFaults McGee

mean the user should be able to enter as many entries as possible without specifying how many in the starting

With the above requirement and with the code you have, you can enter no more than 5 elements to an array.

do{ 
    cin>>x;
    if(x=='\0'){
       flag=true;
    }
    *(A+i)=x;
    i++;
}while(!flag);

Use std::vector instead for the requirement where it implicitly manages memory for you.

You will have to use some sort of variable length datastructure and Vector is the best choice for this since you are working in C++. Therefore, instead of your fixed length array:

int *A;
A=new int[5];

use a vector like this:

std::vector<int> input;

And then to add values to this, use the following:

input.push_back(10);

There is an example on this page about using vectors.

If you use

std::vector<int> a;

then the input becomes simply

while (std::cin >> x)
    a.push_back(x);

Then the user can press ^D (Unix/Linux/etc) or ^Z (DOS/Win) when they've entered all the numbers, or use the program as in:

echo 1 4 22 | program
program < input_file

If you want to have an empty line denote the end of input, then with input validation:

std::string line;
while (getline(std::cin, line))
{
    char c;
    std::istringstream iss(line);
    int x;
    if (iss >> x)
    {
        a.push_back(x);
        char c;
        if (iss >> c)
        {
            std::cerr << "unexpected character '" << c << "' in line '" << line << "', terminating\n";
            exit(EXIT_FAILURE);
        }
    }
    else if (!iss.eof())
        break;  // empty line...
    else
    {
        std::cerr << "unexpected characters in line '" << line << "', terminating\n";
        exit(EXIT_FAILURE);
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM