Help-->While Statement won't break

Condition has been met numerous times.

Page 1 of 1

13 Replies - 1650 Views - Last Post: 18 October 2005 - 06:18 PM Rate Topic: -----

#1 Piperlester   User is offline

  • D.I.C Head
  • member icon

Reputation: 3
  • View blog
  • Posts: 131
  • Joined: 18-October 05

Help-->While Statement won't break

Posted 18 October 2005 - 10:23 AM

while (singleCharFromFile != "|")      //can't get out of this loop
            {
            switch (fieldCounter)
                {
                case 1:
                    {
                    fromFileLastName[fromFileCounter] = srchBuffer[fromFileLastPosition];
                    cout << "In Case 1:\n";
                    }
                    break;
                
                case 2:
                    {
                    fromFileFirstName[fromFileCounter] = srchBuffer[fromFileLastPosition];
                    cout << "In Case 2:\n";
                    }
                    break;
                
                case 3:
                    {
                    fromFileHomePhone[fromFileCounter] = srchBuffer[fromFileLastPosition];
                    cout << "In Case 3:\n";
                    }
                    break;
                    
                case 4:
                    {
                    fromFileCellPhone[fromFileCounter] = srchBuffer[fromFileLastPosition];
                    cout << "In Case 4:\n";
                    }
                    break;
                    
                case 5:
                    {
                    fromFileEmailAddy[fromFileCounter] = srchBuffer[fromFileLastPosition];
                    cout << "In Case 5:\n";
                    }
                    break;
                }
            fromFileCounter++;
            fromFileLastPosition++;
            cout << fromFileCounter << endl;
            cout << fromFileLastPosition << endl;
            cout << fromFileLastName << ", " << fromFileFirstName << "\t" << fromFileHomePhone << "\t" << fromFileCellPhone << "\t" << fromFileEmailAddy << endl;   
            singleCharFromFile[0] = srchBuffer[fromFileLastPosition];
            Sleep(500);
            }

This post has been edited by Piperlester: 18 October 2005 - 10:24 AM


Is This A Good Question/Topic? 0
  • +

Replies To: Help-->While Statement won't break

#2 Amadeus   User is offline

  • g+ + -o drink whiskey.cpp
  • member icon

Reputation: 253
  • View blog
  • Posts: 13,507
  • Joined: 12-July 02

Re: Help-->While Statement won't break

Posted 18 October 2005 - 10:37 AM

What variable type is the singleCharFromFile? Is it a char, or a string?
If a char, you may have modify your condition to
if(singleCharFromFile != '|')


Was This Post Helpful? 0
  • +
  • -

#3 Piperlester   User is offline

  • D.I.C Head
  • member icon

Reputation: 3
  • View blog
  • Posts: 131
  • Joined: 18-October 05

Re: Help-->While Statement won't break

Posted 18 October 2005 - 10:59 AM

It is:
char singleCharFromFile[2];

i tried changing it to '|', but it gives another error:

ISO C++ forbids comparison between pointer and integer
Was This Post Helpful? 0
  • +
  • -

#4 Amadeus   User is offline

  • g+ + -o drink whiskey.cpp
  • member icon

Reputation: 253
  • View blog
  • Posts: 13,507
  • Joined: 12-July 02

Re: Help-->While Statement won't break

Posted 18 October 2005 - 11:03 AM

The problem is you are comparing an array to a character...you either need to make the varaible a char, or use a string compare function to get your condition...like strcmp().
Was This Post Helpful? 0
  • +
  • -

#5 Piperlester   User is offline

  • D.I.C Head
  • member icon

Reputation: 3
  • View blog
  • Posts: 131
  • Joined: 18-October 05

Re: Help-->While Statement won't break

Posted 18 October 2005 - 11:28 AM

Is it possible to copy from fileInput[x] to aChar?
If so, how?
Was This Post Helpful? 0
  • +
  • -

#6 Amadeus   User is offline

  • g+ + -o drink whiskey.cpp
  • member icon

Reputation: 253
  • View blog
  • Posts: 13,507
  • Joined: 12-July 02

Re: Help-->While Statement won't break

Posted 18 October 2005 - 11:35 AM

Why do you need singleCharFromFile to be an array of two characters? I don't want to advise you to change it if you have a valid reason. If there is no compelling reason, then declare singleCharFromFile as a char:
char singleCharFromFile;


and use the single quotes in your condition:
if(singleCharFromFile != '|')


If you have a reason for making it two characters long, then include the string.h library, and use the strcmp() function
char str1[]="|";
if(strcmp(singleCharFromFile,str1)!=0)


Either method should work, depending on how you set up the variable.
Was This Post Helpful? 0
  • +
  • -

#7 Piperlester   User is offline

  • D.I.C Head
  • member icon

Reputation: 3
  • View blog
  • Posts: 131
  • Joined: 18-October 05

Re: Help-->While Statement won't break

Posted 18 October 2005 - 11:49 AM

It's still not working... here's the entire chunk of code that is being problematic.

char srchBuffer[256];
char srchFirstX[256];
int getFirstX = 0;
int srchFileLine = 0;
int fromFileLastPosition = 0;
int fieldCounter = 1;
int fromFileCounter = 0;
char singleCharFromFile[] = "";
char pipeHolder[] = "1";

char fromFileLastName[256];
char fromFileFirstName[256];
char fromFileHomePhone[256];
char fromFileCellPhone[256];
char fromFileEmailAddy[256];
ifstream searchingFile (fNamePointer);
cout << "The following entries matched your query of: " << srchQuery << endl;    
  while (! searchingFile.eof())
  {
  srchFileLine++;
  getFirstX = 0;
  searchingFile.getline (srchBuffer,256);
   
  //this loop will fill the variable srchFirst3 with the first 3 characters from the file line.
    while (getFirstX < srchQueryLen) 
    {
    srchFirstX[getFirstX] = srchBuffer[getFirstX];
    getFirstX++;
    }
  srchFirstX[getFirstX] = '\0';
    if (strcmp(srchFirstX,srchQuery) == 0 && (srchFileLine >= 3))
    {
    fromFileLastPosition = 0;
        while (fieldCounter <= 5)
        {
        fromFileCounter = 0;
        singleCharFromFile[0] = srchBuffer[fromFileLastPosition];
            if (strcmp(singleCharFromFile,pipeHolder) == 0)
            {
            fieldCounter++;
            fromFileLastPosition++;
            }
            else if (strcmp(singleCharFromFile,pipeHolder) != 0)
            {
            while (strcmp(singleCharFromFile,pipeHolder) != 0)      //can't get out of this loop
            {
            switch (fieldCounter)
                {
                case 1:
                    {
                    fromFileLastName[fromFileCounter] = srchBuffer[fromFileLastPosition];
                    cout << "In Case 1:\n";
                    }
                    break;
                
                case 2:
                    {
                    fromFileFirstName[fromFileCounter] = srchBuffer[fromFileLastPosition];
                    cout << "In Case 2:\n";
                    }
                    break;
                
                case 3:
                    {
                    fromFileHomePhone[fromFileCounter] = srchBuffer[fromFileLastPosition];
                    cout << "In Case 3:\n";
                    }
                    break;
                    
                case 4:
                    {
                    fromFileCellPhone[fromFileCounter] = srchBuffer[fromFileLastPosition];
                    cout << "In Case 4:\n";
                    }
                    break;
                    
                case 5:
                    {
                    fromFileEmailAddy[fromFileCounter] = srchBuffer[fromFileLastPosition];
                    cout << "In Case 5:\n";
                    }
                    break;
                }
            fromFileCounter++;
            fromFileLastPosition++;
            cout << fromFileCounter << endl;
            cout << fromFileLastPosition << endl;
            cout << fromFileLastName << ", " << fromFileFirstName << "\t" << fromFileHomePhone << "\t" << fromFileCellPhone << "\t" << fromFileEmailAddy << endl;   
            singleCharFromFile[0] = srchBuffer[fromFileLastPosition];
            Sleep(500);
            }
            }
        Sleep(1000);
        }
                
            
    }
  }

Was This Post Helpful? 0
  • +
  • -

#8 Amadeus   User is offline

  • g+ + -o drink whiskey.cpp
  • member icon

Reputation: 253
  • View blog
  • Posts: 13,507
  • Joined: 12-July 02

Re: Help-->While Statement won't break

Posted 18 October 2005 - 11:57 AM

Try just declaring the singleFileFromchar as a char...no brackets
char singleCharFromFile;


then make this your condition:
if(singleCharFromFile != '|')


As an FYI, your pipeHolder array contains a 1, not a pipe.
Was This Post Helpful? 0
  • +
  • -

#9 Piperlester   User is offline

  • D.I.C Head
  • member icon

Reputation: 3
  • View blog
  • Posts: 131
  • Joined: 18-October 05

Re: Help-->While Statement won't break

Posted 18 October 2005 - 12:22 PM

Ok... it's semi-functional now. It keeps adding a smiley and occasionally removes a char or two. Would that be caused by a lack of \0 in the string?

As well, how do I get the strings to 'zero' off after the cout << lots of vars? Currently, it just outputs the first 'line' of input x times (where x is the number of lines matching the query).
Was This Post Helpful? 0
  • +
  • -

#10 Amadeus   User is offline

  • g+ + -o drink whiskey.cpp
  • member icon

Reputation: 253
  • View blog
  • Posts: 13,507
  • Joined: 12-July 02

Re: Help-->While Statement won't break

Posted 18 October 2005 - 12:25 PM

Can you give me an example? Section of code, followed by the output that you refer to?
Was This Post Helpful? 0
  • +
  • -

#11 Piperlester   User is offline

  • D.I.C Head
  • member icon

Reputation: 3
  • View blog
  • Posts: 131
  • Joined: 18-October 05

Re: Help-->While Statement won't break

Posted 18 October 2005 - 12:35 PM

cout << "The following entries matched your query of: " << srchQuery << endl;    
  while (! searchingFile.eof())
  {
  srchFileLine++;
  getFirstX = 0;
  searchingFile.getline (srchBuffer,256);
   
  //this loop will fill the variable srchFirst3 with the first 3 characters from the file line.
    while (getFirstX < srchQueryLen) 
    {
    srchFirstX[getFirstX] = srchBuffer[getFirstX];
    getFirstX++;
    }
  srchFirstX[getFirstX] = '\0';
    if (strcmp(srchFirstX,srchQuery) == 0 && (srchFileLine >= 3))
    {
    fromFileLastPosition = 0;
        while (fieldCounter <= 5)
        {
        fromFileCounter = 0;
        singleCharFromFile = srchBuffer[fromFileLastPosition];  
            if (singleCharFromFile == '|')
            {
            fieldCounter++;
            fromFileLastPosition++;
            }
            else if (singleCharFromFile != '|')
            {
            while (singleCharFromFile != '|')     
            {
            switch (fieldCounter)
                {
                ...All Switch Stuff Here
                }
            fromFileCounter++;
            fromFileLastPosition++;
            singleCharFromFile = srchBuffer[fromFileLastPosition];
            }
            }
        }
    cout << fromFileLastName << ", " << fromFileFirstName << "\t" << fromFileHomePhone << "\t" << fromFileCellPhone << "\t" << fromFileEmailAddy << endl;               
    }
  }



If the user has input 'Bow', the bottom cout will print:
Bowlendy:), George? 12345678910 12345678910 [email protected]
Bowlendy:), George? 12345678910 12345678910 ...etc

It's only the first two variables that add the extra chars. But, it will print the same thing 4 times. Regardless if all the entries are not the same.

The file holds:
Bowlendy|George|12345678910|12345678910|[email protected]|
Bowlendy|Greg|12345678910|12345678910|[email protected]|
Bowlendy|Randy|12345678910|12345678910|[email protected]|
Bowlen|Bob|12345678910|12345678910|[email protected]|
Was This Post Helpful? 0
  • +
  • -

#12 eLliDKraM   User is offline

  • Pepè Le Pewn
  • member icon

Reputation: 7
  • View blog
  • Posts: 6,565
  • Joined: 13-August 05

Re: Help-->While Statement won't break

Posted 18 October 2005 - 02:50 PM

Quote

Ok... it's semi-functional now. It keeps adding a smiley and occasionally removes a char or two. Would that be caused by a lack of \0 in the string?


\1 is a smiley face, if that's what you're asking.
Was This Post Helpful? 0
  • +
  • -

#13 Piperlester   User is offline

  • D.I.C Head
  • member icon

Reputation: 3
  • View blog
  • Posts: 131
  • Joined: 18-October 05

Re: Help-->While Statement won't break

Posted 18 October 2005 - 02:57 PM

Good to know what \1 is :)

I sort of managed to solve the problem. One done, many, many to go.

Thanks for all the replies.
Was This Post Helpful? 0
  • +
  • -

#14 Piperlester   User is offline

  • D.I.C Head
  • member icon

Reputation: 3
  • View blog
  • Posts: 131
  • Joined: 18-October 05

Re: Help-->While Statement won't break

Posted 18 October 2005 - 06:18 PM

Ok, entire program working now. Thanks again for all the help.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1