-1
#include <iostream>
#include <string>

using namespace std;
int main()
{
    string emailAddress, computerTyp, temp;
    std::string address;
    cout << "ITSx IT Solutions\n" << endl;
    cout << "enter your email address : " << endl;
    cin >> emailAddress;
    std::cout << "enter address : " << std::endl;
    
    //getline(cin, address);
    //cin >> address;
    for (int i = 0; i < 2; i++)
    {
        while (std::cin >> temp)
        {
            getline(std::cin, temp, ' ');
            // use x
            address += temp;
        }
    }



    //cin >> address;
    cout << "Enter the type of computer you need  : "<< std::endl;
    cin >> computerTyp;
    cout << "Email Address : " << emailAddress << " Address : "<< address << " Computer Type : " << computerTyp << endl;
    return 0;
}

I Want To Get To Get Line Separated With Spaces From User Input. And Display It On The Console.

So Whenever I Try To Add Address It Skips That Line And Goes To Computer Types Option I

Ulrich Eckhardt
  • 16,572
  • 3
  • 28
  • 55
  • Providing examples of what you consider an "Address" would be helpful. Whether it is 2, 3, 4 lines, etc.. and the whitespace included. Your use of both `std::cin` and `getline(std::cin, temp, ' ')` is curious. What are you attempting to accomplish there? Why are you mixing `cin` and `std::cin`? Just do away with `using namespace std;` and be consistent. See [Why is “using namespace std;” considered bad practice?](https://stackoverflow.com/q/1452721/364696) – David C. Rankin Jul 11 '21 at 09:26
  • Los Angeles 123 St. – azquick store Jul 11 '21 at 09:27
  • Why not simply read the address into a string with `getline(std::cin, address)`? (you can separate the parts later -- but for your purpose here, that seems the easiest way to go) – David C. Rankin Jul 11 '21 at 09:29
  • Hey david. ist skipping the first set of character and starts from the second set like when i enter Los Angeles 123 St. it displays "Angeles 123 St." – azquick store Jul 11 '21 at 09:30
  • Of course it is, you have `while (std::cin >> temp)` before `getline()` that is stripping off `Los`... – David C. Rankin Jul 11 '21 at 09:32
  • So what is the answer. Am stuck – azquick store Jul 11 '21 at 09:34
  • Get rid of `while (std::cin >> temp)` (the whole loop) and just `getline(std::cin, address)` – David C. Rankin Jul 11 '21 at 09:36
  • And read [Why does std::getline() skip input after a formatted extraction?](https://stackoverflow.com/questions/21567291/why-does-stdgetline-skip-input-after-a-formatted-extraction) – molbdnilo Jul 11 '21 at 09:38
  • Now am getting this https://prnt.sc/1a7ryrc – azquick store Jul 11 '21 at 09:40
  • Its skipping the address part – azquick store Jul 11 '21 at 09:41
  • Your problem is mixing `std::cin` (which leaves a `'\n'` unread in `stdin` followed by `getline()` that reads up to the first `'\n'`. Simply use `getline()`. I'll drop an example. – David C. Rankin Jul 11 '21 at 09:46

2 Answers2

2

The problem you are having is a common one for new C++ programmers. You are mixing operator>> and std::getline() without accounting for the '\n' line-ending that operator>> leaves unread in stdin when the user types Enter. On your next call to getline(), it sees the '\n' left by operator>>, and since getline() stops reading by default when it encounters a '\n', you perceive that is "Skipping" input.

It's not skipping anything, it's just seeing the '\n' and stops reading there, filling the string as an empty-string. That is why you tried to kludge it by adding a ' ' delimiter for getline(), which obliterates the benefit from using getline() to begin with.

Instead, simply use getline() for all inputs, e.g.

#include <iostream>
#include <string>

int main()
{
    std::string emailAddress{}, computerTyp{}, address{};
    
    std::cout << "ITSx IT Solutions\n\nenter e-mail address :\n";
    if (!getline (std::cin, emailAddress))
        return 1;
    
    std::cout << "enter address :\n";
    if (!getline (std::cin, address))
        return 1;
    
    std::cout << "Enter the computer type :\n";
    if (!getline (std::cin, computerTyp))
        return 1;
    
    std::cout << "\nEmail Address : " << emailAddress << 
                 "\nAddress       : " << address << 
                 "\nComputer Type : " << computerTyp << '\n';
}

Example Use/Output

$ ./bin/compinfo
ITSx IT Solutions

enter e-mail address :
foo@bar.com
enter address :
Los Angeles 123 St.
Enter the computer type :
Super Fast One

Email Address : foo@bar.com
Address       : Los Angeles 123 St.
Computer Type : Super Fast One

Look things over and let me know if you have further questions.

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
David C. Rankin
  • 81,885
  • 6
  • 58
  • 85
0
#include <iostream>
#include <string>

using namespace std;
int main()
{
    string emailAddress, computerTyp, address;
    cout << "ITSx IT Solutions\n" << endl;
    cout << "enter your email address : " << endl;
    cin >> emailAddress;
    cout << "enter address : " << endl;
    
 
    for (int i = 0; i < 2; i++)
    {
        getline(cin, address);
    }
    cout << "Enter the type of computer you need  : "<< endl;
    for (int i = 0; i < 1; i++)
    {
        getline(cin, computerTyp);
    }
    cout << "Email Address : " << emailAddress << " Address : "<< address << " Computer Type : " << computerTyp << endl;
    return 0;
}

VUSUAL STUDIO 2019 OUTPUT

ITSx IT Solutions

enter your email address :
johndoe@gmail.com
enter address :
California 123 St Corner
Enter the type of computer you need  :
Hp Pavillion 24gb Ram 1024 gb HDD
Email Address : johndoe@gmail.com Address : California 123 St Corner Computer Type : Hp Pavillion 24gb Ram 1024 gb HDD

C:\Users\Win 10\Desktop\ITSx IT Solutions Windows Console application C++ Programming\Debug\ITSx IT Solutions Windows Console application C++ Programming.exe (process 4904) exited with code 0.
To automatically close the console when debugging stops, enable Tools->Options->Debugging->Automatically close the console when debugging stops.
Press any key to close this window . . .

Output Screenshot

  • Better, but you are still using loops as ugly hacks to handle the `'\n'`. Don't do that. The proper way to handle emptying `stdin` is to use `std::cin.ignore`, e.g. `std::cin.ignore (std::numeric_limits::max(), '\n');`. So after a `std::cin` and before using `getline()` call `std::cin.ignore` -- don't use loops... `#include` as well for the max size to read, e.g. `std::numeric_limits::max()`. See [std::basic_istream::ignore](https://en.cppreference.com/w/cpp/io/basic_istream/ignore) – David C. Rankin Jul 11 '21 at 10:03
  • Okay David, Noted!! – azquick store Jul 11 '21 at 10:08
  • You have done the most important part -- got it working! (good job), now you can refine your understanding and the code as you go forward. Keep the [cppreference.com](https://en.cppreference.com/) link handy and -- use it! It is the best C++ reference on the internet. Go to it for every feature you use -- and your errors will become fewer and farther between. – David C. Rankin Jul 11 '21 at 10:11
  • Cool David. Thanks for the help I will Check out - https://en.cppreference.com/w/ – azquick store Jul 11 '21 at 10:17