hello! im trying to make a program where entered values should be displayed in a tabulated form. however, as far as i know, everytime we enter a value and press ENTER, the cursor goes to the next line. is there any way where in if a user enters a value and press ENTER, the cursor will go to the next tab only and not to the next line?


i hope you understand my problem. thank you btw.

Dani AI

Generated

Short answer for : you cannot make the Enter key stop producing a newline in a normal console. Enter is handled by the terminal/keyboard driver and sends a line terminator to the program. That means you should treat input and output separately: accept values (Enter ends a line) and then display them in columns using tabs or fixed-width formatting.

As suggested, \t works for output but tab stops are variable and not reliable for neat alignment. As noted, input routines wait for Enter; you can read several values per line, or read one line and parse it. A common, portable pattern is: collect tokens (for example with std::getline + std::istringstream) and print rows using manipulators from <iomanip> like std::setw and std::left so columns line up regardless of character widths.

Example (read lines, parse tokens, print aligned columns):

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

int main() {
  std::string line;
  std::cout << std::left << std::setw(12) << "Account" << std::setw(10) << "Amount" << '\n';
  while (std::getline(std::cin, line)) {
    if (line.empty()) break; // stop on blank line
    std::istringstream iss(line);
    std::string account;
    double amount;
    if (iss >> account >> amount) {
      std::cout << std::left << std::setw(12) << account << std::setw(10) << amount << '\n';
    }
  }
}

If you need real interactive form behavior (press Enter to move focus to the next on-screen field instead of producing visible newlines), use a console UI library (ncurses) or platform APIs (Windows Console functions like SetConsoleCursorPosition). For formatting helpers see the C++ reference for std::setw (cppreference) and for std::getline (cppreference).

Recommended Answers

All 4 Replies

a \t makes it move horizontally

can i have the proper coding for it? because when i add \t in cin,, there is an error.

thanx for the reply

cin attempts to read an integer from standard input. It waits until the user presses <Enter> and then attempts to convert the values entered and stores them into your variables.

You can read multiple values at a time, by storing them in different variables. ie

cin >> accountno >> amount ;

I don't know a way by which an <Enter> will not take you to the next line.

thanx for the reply!! ill try if it will work. :)

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.