Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Simple Question

Posted on 2004-09-08
3
Medium Priority
?
291 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 100 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 100 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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

636 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