Note that this code works just fine when name is a regular string instead of a c-string.
I made this test program to figure out what was causing problems with my project. Turns out that the code for name that I put works just fine, but it messes up when I add the coding that follows which asks if you want to hear the rules... This is the code:

#include<iostream>
#include<iomanip>
#include<fstream>
#include<string>
#include<cstdlib>
#include<ctime>
#include<cctype>
using namespace std;

void show_name(string name)
{
    cout << name << endl;
    cout << "done..." << endl;
}

void show_rules()
{

}

int main()
{
    char name[4];
    cout << "\nPlease enter your initials (No more than 3): ";
    cin.getline( name, 4 );
    for( int up = 0; up < 4; up++ )
        name[up] = toupper(name[up]);
    while( strlen( name ) > 3 || strlen( name ) < 1 )
    {
        if( strlen( name ) > 3 )
            cout << "Please enter no more than 3 initials!" << endl;
        else if( strlen( name ) < 1 )
            cout << "Please enter at least 1 initial!" << endl;
        cout << "Please enter your initials: ";
        cin.getline( name, 4 );
        for( int up = 0; up < 4; up++ )
            name[up] = toupper(name[up]);
    }
    char rules;
    cout << "Would you like to hear the rules? (Y/N)" << endl;
    cin >> rules;
    rules = toupper(rules);
    while( rules != 'N' && rules != 'Y' )
    {
        cout << "Please enter Y for Yes, or N for No!"
             << endl;
        cout << "Would you like to hear the rules? (Y/N)" << endl;
        cin >> rules;
        rules = toupper(rules);
    }
    if( rules == 'Y' )
        show_rules();
    show_name( name );
    return 0;
}

There is extra stuff there because this is a snippet of a program I'm working for a class, the main interest is in the function main(). Without the "char rules;" line and on, when it moves on to the function show_name, the name displays correctly. When the lines "char rules;" and on are added, after entering a three digit, only three digit, 2 and 1 work fine, it goes into an infinite loop saying:

Please enter Y for Yes, or N for No!
Would you like to hear the rules? (Y/N)
Please enter Y for Yes, or N for No!
Would you like to hear the rules? (Y/N)
Please enter Y for Yes, or N for No!
Would you like to hear the rules? (Y/N)
Please enter Y for Yes, or N for No!
Would you like to hear the rules? (Y/N)
Please enter Y for Yes, or N for No!
Would you like to hear the rules? (Y/N)
Please enter Y for Yes, or N for No!
Would you like to hear the rules? (Y/N)
Please enter Y for Yes, or N for No!
Would you like to hear the rules? (Y/N)

can anyone please help me figure this out?

If you're having trouble with an infinite loop, why don't you investigate the conditions of that loop?

while( rules != 'N' && rules != 'Y' )

it sais that the loop will go on if rules is but the char 'N' or 'Y', meaning anything else than those 2 chars will provoke an infinite loop.
But, if you want your loop to finish when anything but 'y' or 'n' are introduced, you should use this condition:

 while( rules == 'N' || rules == 'Y')

That loop is there to validate the entry for rules, making sure that the person is asked to re enter the response if their previous response isn't N or Y.
While the logic within the paranthesis is true, the loop will continue to replay. so if rules is not 'N' AND rules is not 'Y' then it will ask the player the question again, because the only way for the logic within the paranthesis to be true is if both of those statements are true, which are only true if you choose a different character that isn't N or Y. if I change it to your suggestion, it will repeat the question over and over again because you are inputting N, or Y, but the coniditions are testing that.
Changing the codition to that only gave me this.

Please enter your initials (No more than 3): smj
Would you like to hear the rules? (Y/N)
SMJ
done...

Where I wasn't even given a choice of whether I would like to hear the rules or not. I know its not the loop exactly, I feel its more of something that is saved on the buffer of what I have previously entered for "cin.getline( name, 4 );" but I can't figure out how to fix it.

EDIT:
Commented some stuff down, and basically narrowed down the problem to cin.getline( name, 4 );" not sure how to fix it though :/

#include<iostream>
#include<iomanip>
#include<fstream>
#include<string>
#include<cstdlib>
#include<ctime>
#include<cctype>
using namespace std;

void show_name(string name)
{
    cout << name << endl;
    cout << "done..." << endl;
}

void show_rules()
{

}

int main()
{

    char name[4];
    cout << "\nPlease enter your initials (No more than 3): ";
    cin.getline( name, 4 );
    /*
    for( int up = 0; up < 4; up++ )
        name[up] = toupper(name[up]);
    while( strlen( name ) > 3 || strlen( name ) < 1 )
    {
        if( strlen( name ) > 3 )
            cout << "Please enter no more than 3 initials!" << endl;
        else if( strlen( name ) < 1 )
            cout << "Please enter at least 1 initial!" << endl;
        cout << "Please enter your initials: ";
        cin.getline( name, 4 );
        for( int up = 0; up < 4; up++ )
            name[up] = toupper(name[up]);
    }*/
    char rules;
    cout << "Would you like to hear the rules? (Y/N)" << endl;
    cin >> rules;
    rules = toupper(rules);
    while( rules != 'N' && rules != 'Y' )
    {
        cout << "Please enter Y for Yes, or N for No!"
             << endl;
        cout << "Would you like to hear the rules? (Y/N)" << endl;
        cin >> rules;
        rules = toupper(rules);
    }
    if( rules == 'Y' )
        show_rules();
    show_name( name );
    return 0;
}

Edited 3 Years Ago by TexasJr

This article has been dead for over six months. Start a new discussion instead.