string not fully outputted when a type bool function is called.
Hello, all.
I don't like to post anything on boards, but I am stumped here.
I have a program that will solicit a line of text from the user. It will then output the string later in the program (by passing a char * to a function).
The trouble comes in when main() ALSO calls a certain function that returns a type bool variable. When that function is called, the string does not output fully!
Please note that when I assign either 'true' or 'false' to the bool variable in main(), instead of getting the value from the return value of the type
bool function, the string outputs just fine.
What in the world could I possibly be missing?
For any interested, here is the code, with all comments that I thought would be pertinent. Note that if you comment the line:
use_num = use_number();
and uncomment the line:
use_num = true;
Then the program runs fine. How could this seemingly unrelated function call break the output of the string?
Code:
// comparison.cpp
// What changes the length of the string?
#include <iostream>
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.
int main(void)
{
using namespace std;
char * pt_line = new char; // 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";
char ch;
int i = 0;
while((ch = getchar()) != '\n')
{
*(pointer + i) = ch;
i++;
}
}
bool use_number(void)
{
using namespace std;
string usr_in;
cout << "Do you want to use a number? [Y/n] ";
getline(cin, usr_in);
while((usr_in.size() != 1 && usr_in.size() != 0) ||
(usr_in != "y" && usr_in != "Y" && usr_in != "n" && usr_in != "N" && usr_in.size() != 0))
{
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" || usr_in == "Y" || usr_in.size() == 0)
return true;
else if(usr_in == "n" || usr_in == "N")
return false;
else
{
cout << "Error in function use_number(), program is terminating...\n";
exit(EXIT_FAILURE);
}
}