Pointers


"Write a program that calculates how much a person would earn over a period of time if his/her salary is 1 penny for the first day and 2 pennies for the second day, and continues to double each day. The program should ask the user for the number of days. Display a table showing how much the salary was for each day and then show teh total pay at teh end of the period. The output should be displayed in a dollar ammount, not the number of pennies.
*Input vaildation* Do not accept a number less than 1 for hte number of days worked."

#include <iostream>
#include <iomanip>
using namespace std;

int main()
{
int days, salary; // Initiate the counter
double total = 0.0;

//Get integer
cout << "Enter a positive number\n";
cin >> days;
for (; days > 0; days--)
{
(total+=days*2);
}

//Display Total
cout << fixed << showpoint << setprecision(0);
cout << "The salary is $:" << total << endl;
system ("pause");
return 0;
}


How can I convert this program to pointer with functions. HELP!!!
Hello eddiekem,

PLEASE ALWAYS USE CODE TAGS (the <> formatting button) when posting code.
It makes it easier to read your code and also easier to respond to your post.
http://www.cplusplus.com/articles/jEywvCM9/
http://www.cplusplus.com/articles/z13hAqkS/
Hint: You can edit your post, highlight your code and press the <> formatting button.
You can use the preview button at the bottom to see how it looks.

What are you thinking of for using pointers?

The program is small enough that pointers are not necessary, but could be used.

It is very easy to break the program into functions where you could use a pointer to the function, but again there is not much point unless that is what you need to do.

You do need some kind of validation on your input.

Beyond that I would have to load u the program and see how it is working.

Andy
Is a homework that they want that program but with pointers
Hello eddiekem,

I would suggest breaking up what is in main into regular functions and get that to work first then change to function pointers.

You can read about function pointers here: http://www.cplusplus.com/doc/tutorial/pointers/ It is at the bottom of the page.

Hope that helps,

Andy
Topic archived. No new replies allowed.