User-defined function for "for loop"

My program works fine, but I am struggling developing a user-defined function for the for loop. Everything I try won't work, any help would be greatly appreciated.

#include <iostream>
#include <cstdio>
#include <cmath>
#include <iomanip>

using namespace std;

int main()
int counter;
double firstTerm = ' ';
double commRatio = ' ';
double total, numTerms = ' ';
double sum;

cout << "Program will compute terms in a Geometric Series"<<endl<<endl;

cout << "Enter the first term in the series: ";
cin >> firstTerm;

cout<<"Enter the common ratio for the series: ";
cin >> commRatio;

cout<<"Enter the number of terms to use: ";
//while (numTerms < 2)

cout << fixed << showpoint;

cout <<setprecision(4)<< "Terms in series for Common Ration
of "<<commRatio<<" are:"<<endl;
cout << setprecision(9);

for (int counter = 1; counter <= numTerms; counter++ )

sum = firstTerm * pow(commRatio,(counter-1));
total += sum;
cout << setw(20) <<sum<< endl;


system ("pause");
return 0;

What is the purpose to initialize floating numbers with the space character?!

double firstTerm = ' '; 
double commRatio = ' ';
 double total, numTerms = ' ';

As for the function I have not understood what function you need.
Not sure why the pow function is used.
In a geometric series, each term can be derived from the previous one by a simple multiplication by the common ratio.

A question: are you trying to list all of the terms, or just calculate the sum of all the terms?
Okay I got rid of the ' '; and initialized to 0;

I am trying to list the terms.

My delema is now this, any value returned from the function to main is 0, how do I take the input value from the function to main?

double startTerm (double firstTerm, string str1);

double startTerm (double ft, string str1)


double newTerm;

cout<<"Enter first term: ";



while (newTerm > 1)


return newTerm;


if (newTerm <= 0)


cout<< str1 << " " <<endl;

Topic archived. No new replies allowed.