Solved

Simple Question

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Handle Exceptions during instantiation 28 411
c++ getting the first 10 characters of a char* string 11 90
c++ syntax question 9 43
Arduino EDI - Programming language 3 80
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…
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 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 be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

914 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

14 Experts available now in Live!

Get 1:1 Help Now