how can I place all of my program in a while loop
what I want is this:
the user should enter color of pen ,color of brush and then enter name of a shape that want to draw.
my program should give this information and draw a shape
the user have this permission not to enter something as color of pen or brush.
if he doesn't enter something as pencolor or penbrush my program should have default.
my problem is this:
my program can't give 2 string and then do its task.
i mean my program should do :cin.getline(strsentence)
and do some tasks(for example change the color of pen)
and then cin.getline(strsentence)
this time strsentence is circle(x,y,r)
the program should draw a circle with entered coordinate
but it can't do this task
pleasse help me

Dani AI

Generated

Yes — the whole interaction (ask for pen, brush, shape, draw, repeat) can live inside a loop. As noted, the important part is choosing a clear exit condition (EOF, a sentinel like quit, or an explicit "another? y/n"). The typical reason the second getline appears to "skip" is leftover input from a prior formatted extraction or use of C-style buffers; the safer pattern is to use std::string with std::getline consistently, treat an empty line as "use default", and parse the shape text with a std::istringstream.

Common practical rules:

  • Prefer std::getline(std::cin, s) for all interactive string input.
  • After a operator>> use std::cin.ignore(...) or switch to getline for everything.
  • If an empty line means "use default", check s.empty() and substitute the default.
  • Normalize the shape line (replace (, ), , with spaces) then parse tokens with istringstream so both circle 10 20 5 and circle(10,20,5) work.

Example pattern (compact):

#include <iostream>
#include <sstream>
#include <string>

int main() {
  std::string pen, brush, line;
  for (;;) {
    std::cout << "Pen (Enter for 'black'): ";
    if (!std::getline(std::cin, pen)) break;
    if (pen.empty()) pen = "black";

    std::cout << "Brush (Enter for 'none'): ";
    if (!std::getline(std::cin, brush)) break;
    if (brush.empty()) brush = "none";

    std::cout << "Shape (e.g. circle(10,20,5) or 'quit'): ";
    if (!std::getline(std::cin, line) || line == "quit") break;

    for (size_t i = 0; i < line.size(); ++i)
      if (line[i] == '(' || line[i] == ')' || line[i] == ',') line[i] = ' ';

    std::istringstream iss(line);
    std::string shape; iss >> shape;
    if (shape == "circle") {
      double x,y,r;
      if (iss >> x >> y >> r) {
        // drawCircle(x,y,r,pen,brush);
      } else {
        std::cerr << "Bad circle parameters\n";
      }
    } else {
      std::cerr << "Unknown shape\n";
    }
  }
}

Notes and troubleshooting: avoid mixing C-style cin.getline(char*,...) with operator>>; trim whitespace if spaces should count as empty; use a sentinel like quit or detect EOF for batch input; and add clear error messages when parsing fails. As suggested earlier, showing the actual code helps diagnosis, but the pattern above resolves the most common problems with multiple getline calls and default fallbacks.

Recommended Answers

All 3 Replies

Well what have you got written so far? We don't do homework for others!

I don't want you to do my homework.
I just want to know if I can place all of a program in a while loop .
please tell me yes or no.
I tried a lot but couldn 't find a good condition to break the loop

int main() {
  do {
    ...
    your code
    ...
  } while (not some exit condition);
  return 0;
}

OR

int main() {
  while (1) {
    ...
    your code
    ...
    if (some exit condition) { break; }
  }
  return 0;
}
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.