Solved

Simple Question

Posted on 2004-09-08
3
241 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
  • 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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

762 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now