Solved

Simple Question

Posted on 2004-09-08
3
276 Views
Last Modified: 2010-04-01
Hello, I am building an application, Main is as follows:

int main(){
string strEmployeeName;
double dblHoursWorked;
double dblPayRate;
cout << strAppName << " v" << intMainVersion
       << "." << intSubVersion
       << ", revision " << intRevision
       << "\n\n";
cout << "Employee Name: ";
cin >> strEmployeeName;
cout << "Hours Worked: ";
cin >> dblHoursWorked;
cout << "Pay Rate: ";
cin >> dblPayRate;
return 0;
}

all of the problem include files have been included, however, after I input the Employee name and hit enter, the program just ends, it does not ask me for the input of Hours Worked or Pay Rate.


Whats wrong?

-Brian
0
Comment
Question by:BrianGEFF719
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 11

Assisted Solution

by:avizit
avizit earned 25 total points
ID: 12013677
tried the following on my computer , linux

int main(){
string strEmployeeName;
double dblHoursWorked;
double dblPayRate;
cout << "Employee Name: ";
cin >> strEmployeeName;
cout << "Hours Worked: ";
cin >> dblHoursWorked;
cout << "Pay Rate: ";
cin >> dblPayRate;
return 0;
}
 
i.e i removed  the cout which referred to strAppName etc
and it worked fine and it asked for name , hours worked and pay rate  just fine

0
 
LVL 11

Expert Comment

by:avizit
ID: 12013688
so obviously your code is fine , maybe something else is wrong ..
which platform you are working on ?
0
 
LVL 4

Accepted Solution

by:
pankajtiwary earned 25 total points
ID: 12013919
Just beautifying your code for readability:-

#include <iostream>
#include <string>

using namespace std;

int main(){
      string strEmployeeName;
      double dblHoursWorked;
      double dblPayRate;

      cout << "Employee Name: ";
      // cin >> strEmployeeName;    change this
         getline(cin, strEmployeeName, '\n');    // This will work perfectly fine.

      cout << "Hours Worked: ";
      cin >> dblHoursWorked;

      cout << "Pay Rate: ";
      cin >> dblPayRate;

      return 0;
}

The code is running fine as long as the employee name is one word. You must be having problem when you try to enter something like "firstname lastname". The problem is in C/C++ the space is taken as a string delimiter, so the strEmployeeName only takes the firstname and the lastname is given to succeessive cin.

If you want so that your program can take input muti words names, then use getline(). Change your "cin >> strEmployeeName;" to  getline(cin, strEmployeeName, '\n'); This will read until it finds a newline.

Cheers!!!
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

738 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question