That code needs some major reworking. That is a very nice mix of C and C++. I say pick one and go for it. I made some changes but it needs a re-write.
Code:
// comparison.cpp
// What changes the length of the string?
#include <iostream>
#include <string>
#include <cctype>
#include <cstdio>
using namespace std;
void get_a_string(char *);
void print_a_string(char *, int i = 0);
bool use_number(void); // This seems to be the "trouble causing" function, in that,
// when it is called, the string pointed to by 'pt_line'
// seems to decrease in length.
void ToUpper(string &toUp);
int main(void)
{
using namespace std;
//This is only allocating one char variable
//char * pt_line = new char;
char *pt_line = new char[50]; // Get memory to put the string of text in.
get_a_string(pt_line); // Solicit the string of text from the user,
// and store it in the memory address pointed
// to by 'pt_line'.
bool use_num;
//================================================== =====================================
// We can get a value for the variable 'use_num' two different ways.
//
// If we use the return value of the function 'use_number(void)' as the value for
// 'use_num', then the string pointed to by 'pt_line' does not output correctly. Only
// the first part of the line is outputted.
//
// If, however, we simply _assign_ a value to 'use_num', then the string IS outputted
// correctly (its entire length is shown).
//
// So, uncomment out only ONE of the following lines of code, to make your choice.
//================================================== =====================================
use_num = use_number(); // This way will cause the program to print out only
// the first part of the string.
//use_num = true; // This (other way) will cause the string to be
// properly outputted.
print_a_string(pt_line);
return 0;
}
// Remember, the function prototype for this function is this:
//
// void print_a_string(char *, int i = 0);
//
// The idea is, that if only a char * is passed to this function, then the line of text
// pointed to by the char * will be outputted only ONCE.
//
// However, if a second argument, of type int is passed to this function as well, and
// it has a non-zero value, then the string pointed to by the type char * argument
// be outputted a number of times equal to the number of times the function has been called by
// main().
void print_a_string(char * pointer, int i)
{
using namespace std;
static int times = 0; // the number of times this function
// has been called.
times++;
if(i == 0)
cout << pointer << endl;
else
{
int j = 0;
while(j < times)
{
cout << pointer << endl;
j++;
}
}
}
void get_a_string(char * pointer)
{
using namespace std;
cout << "Please enter a line of text:\n";
//Maybe fgets?
/*
while((ch = getchar()) != '\n')
{
*(pointer + i) = ch;
i++;
}
*/
fgets(pointer, 50, stdin);
}
bool use_number(void)
{
using namespace std;
string usr_in;
cout << "Do you want to use a number? [Y/n] ";
getline(cin, usr_in);
//Save some comparisons
ToUpper(usr_in);
//You don't need to check for size == 0 since if it doesn't == Y or N
//then it is no good
while(usr_in != "Y" && usr_in != "N")
{
cout << "\t## size of input == " << usr_in.size() << endl;
cout << "Oh, c'mon now... Enter a y or an n...\n";
cout << "Do you want to use a number? [Y/n] ";
getline(cin, usr_in);
}
if(usr_in == "Y")
return true;
if(usr_in == "N")
return false;
cout << "Error in function use_number(), program is terminating...\n";
exit(EXIT_FAILURE);
}
void ToUpper(string &toUp)
{
//This makes a std::string uppercase
for(size_t i = 0; i < toUp.length(); i++)
{
toUp[i] = toupper(toUp[i]);
}
}