how to get a string using array?
and how can I get input in this format
string|12.3 12.23 |
example;with no space between the lines and when user enter 0 it will exit
david|12.3 24.2|
Peter|12.4 24.2|

Dani AI

Generated

A clear approach: read each input line, stop when the user types a single sentinel like 0 on its own line, parse the line into three pieces (name, the numbers field, the trailing bar), convert the whitespace-separated tokens inside the numbers field to doubles, and store each record in a resizable container. was right to push for reading whole lines; was right about collecting parsed records. The gaps to fill are: robust delimiting (handle extra spaces), a clean sentinel check, and safe number conversion.

Key steps to implement

  • Read a full input line and trim leading/trailing spaces.
  • If the trimmed line equals "0" (or your chosen sentinel) break the loop.
  • Find the first and second | characters. Treat missing bars as a parse error.
  • Extract the name (left of first |) and the numbers substring (between the two |), trim both.
  • Use an input string stream to read doubles from the numbers substring; push each into a vector for that record.
  • Store each parsed record in a vector (or other container) so you can process them later.

Small, practical parse helper (drop into your code)

struct Entry {
    std::string label;
    std::vector<double> values;
};

static std::string trim(const std::string& s) {
    auto l = s.find_first_not_of(" \t");
    if (l == std::string::npos) return "";
    auto r = s.find_last_not_of(" \t");
    return s.substr(l, r - l + 1);
}

bool parseLine(const std::string& line, Entry& out) {
    auto a = line.find('|');
    if (a == std::string::npos) return false;
    auto b = line.find('|', a + 1);
    if (b == std::string::npos) return false;
    out.label = trim(line.substr(0, a));
    std::istringstream iss(trim(line.substr(a + 1, b - a - 1)));
    double v;
    while (iss >> v) out.values.push_back(v);
    return true;
}

Troubleshooting notes: if you mix formatted extraction (>>) with line-based reads you can get an extra newline—use std::getline consistently or clear the leftover newline with cin.ignore(). Check for malformed lines and report the problem back to the user instead of silently skipping them. This combination keeps input in one line, supports any count of numbers per name, and cleanly stops on a single 0 line as you requested.

Recommended Answers

All 7 Replies

Yeah... what?

So how can I do it?
the input should be like this:
david | 12.2 23.4 324.5 |
Jack |12.3 23.4 22.3 54.3 |

I don't know how many will the user enter names or data?
I should use an escape word like EXIT so when user types exit it goes to another function?
If I use cin.getline it skips to next line making the format this
david
| 12.23 34.3 344.3 |
it all should be in one line
plz help

Looks like English isn't your native language. Unforturnately, that means it is difficult for me to understand what you are trying to do.

getline() will accept input from the keyboard (or other source) and store it in a string variable. It has nothing to do with how the value of the variable is displayed on the screen or how it is stored in file or how it is printed on paper.

#include<iostream>
#include<string>
using namespace std;
int main()
{
   string firstInput;
   string secondInput;
   cout << "enter the following string.  DON'T push the ENTER key until the entire string is entered:   david | 12.2 23.4 324.5 |";
   getline(cin, firstInput);
   cout << "enter the following string.  DON'T push the ENTER key until the entire string is entered: Jack |12.3 23.4 22.3 54.3 |";
  getline(cin, secondInput);
  cout << firstInput;
  cout << '\n';  //comment out this line to put both inputs on the same line
   cout << secondInput;
   cin.get();
   return 0;
}

"

people often have to wait during ..

The user should be prompted to enter as many names and waiting time as he desires in the following
format: name | 1.2 3.4 3.4 … |

?

Use a list or a vector or some other self expanding container to hold all the inputs. Use a loop to get the input. I'd still use string to hold each input.

vector<string>inputs;
string input;
char answer;
bool getMoreInput = true;
while(getMoreInput)
{
   //tell user how to enter input here
   //get input here
   //add input to inputs here
   //ask user if they want to enter another input
   //if answer is 'n' then change value of getMoreInput here
}

I don't know how to use vectors!
regarding using string!Strings doesn't work fine with this, because I don't know how many times the user will enter data!

I don't know how to use vectors!

Then learn how to use vectors:
http://www.dreamincode.net/forums/showtopic33631.htm

regarding using string!Strings doesn't work fine with this, because I don't know how many times the user will enter data!

That's why you use a vector. You may want to set up a struct:

struct record
{
    string name;
    vector <double> waitingtimes;
};

vector <record> records;

You'll have to parse each line to make a record out of something like this:

Jack |12.3 23.4 22.3 54.3 |

Read each line in as a string, then parse the string to get the components (basically what Lerner is suggesting, but one step further).

The atof or strtod functions may be useful when converting from strings to doubles.

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.