Need Help with Business Card Style Coding...

new to c++, and prof. assigned to write a business card style program, my codes are below, but it says variable "rate" and "hour" are not initialized, and it seems theres something wrong with the getline(cin string), please use simple codes and explain.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66

#include <iostream>
#include <string>
using namespace std;
int main()
	
{
	double payroll;
	double rate; //says being used without being initialized
	double hour; //also says being used without being initialized
	payroll = hour * rate;
	string addr;
	string city;
	string state;
	string zipcode;
	string first;
	string last;
	string title;
	string company;
	
	
	cout <<"Please Enter Your Personal Information Below to Generate Your Business Card..."<<endl;
	cout << "First Name: ";
	cin >> first;
	
	cout << "Last Name: ";
	cin >> last;
	
	cout << "Job Title: "; //this line doesn't break to a new line
	getline(cin,title);
	
	cout << "Company Name: "; // it shows like Job Title: Company Name:
	getline(cin,company);

	cout << "Door Number and Street Name: ";
	getline (cin, addr);

	cout << "City: ";
	getline(cin,city);

	cout << "State: ";
	getline(cin,state);
	
	cout << "Zip Code: ";
	cin >> zipcode;

	cout << "Hourly Paid Rate in Dollar: ";
	cin >> rate;

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

	cout <<"You Entered..." <<endl;
	cout <<"==============================================" <<endl;
	cout << first << " " << last << endl; // I want to make this line show in uppercase
	cout << title <<" of " << company <<endl; // This line in uppercase also
	cout << addr << endl;
	cout << city << " " << state << " " << zipcode << endl;

	cout << "Your payroll is: " << payroll <<endl;


	system("pause");
	return 0;

}
Last edited on
Try changing
1
2
3
4
5
	cout << "First Name: ";
	cin >> first;
	
	cout << "Last Name: ";
	cin >> last;


to

1
2
3
4
5
	cout << "First Name: ";
	getline(cin, first);
	
	cout << "Last Name: ";
	getline(cin, last);


That should take some newline characters out of the stream.

*Edit
Change zipcode to a string or use getline(cin, zipcode) for that as well.
Last edited on
how about the initialization of variables "rate" and "hour" ?

it says they are being used without being initialized
I'm pretty sure that it's because there are newline characters in the stream. Try the above.
tried, but still shows errors about "rate" and "hour" not being initialized.
Look at line 11. What is currently inside of hour, and what is currently inside of rate? Remember- the code is being completed a bit like a list. Payroll isn't being "defined" as the product of the two for every instance of those being updated, but rather is being defined as such for the instance of hour and rate at the beginning of the code.
ya, after i put line 11 to 52, it runs like a charm, thank you guys
one last question, how do i make line 55 and 56 cout in upper case ?
closed account (18hRX9L8)
Add this after line 3.
#include <ctype.h> // for toupper(int)

Add this before main function.
1
2
3
4
5
6
7
// Change each character in str to an upper character. Return the new str.
std::string Upperify(std::string str) {
	for(unsigned counter = 0; counter < str.length(); counter++) {
		str[counter] = toupper(str[counter]);
	}
	return str;
}


Usage:
std::cout << Upperify("hello world");
Last edited on
You could also try transform(s.begin(), s.end(), s.begin(), toupper);
thx guys, i used
 
transform(s.begin(), s.end(), s.begin(), toupper);


and now everything is seem to be working well.
and just being curious, do i have to do it like shown above one by one ? or is there a way to do it all together ? for the "first","last","title", and "company" ?
1
2
3
4
5
6
7
8
9
void toUpper(string &s)
{
    transform(s.begin(), s.end(), s.begin(), toupper);
}

toUpper(first);
toUpper(last);
toUPPer(title);
toUpper(company);


That's the best I've got.
Topic archived. No new replies allowed.