Hi

I used an variable int i; in a faunction block as it is in a code below.

#includes....
 
int i;
 
int function()
{
while(charbuff[i]!=' ')
       {
            ofstream stream("file.lng", ios::app)
            stream << charbuff[i];
            i++;
            stream.close();
        }
return 0;
}
 
main()
{
      ........... some code
      function();                                  /*call function */
      cout << i;                                  /* the i is set to 0*/
      ........... some code
 
}

I declared the variable as a global variable (??) I think.
It is declared out of all functions right after the #include lines.

The problem is> I need to have the int i as it is, after it is leaving the above function (for example 5). I want to test the same character from charbuff array in other function.
It always reset it to int i=0.
Why the i does not stay the same as the function done its job.

thanks for help

why don't you make i a local variable and function() return its value.

#includes....
 
 
int function()
{
 int i = 0; 
while(charbuff[i]!=' ')
       {
            ofstream stream("file.lng", ios::app)
            stream << charbuff[i];
            i++;
            stream.close();
        }
return i;
}
 
main()
{
      ........... some code
  int i = function();                                  /*call function */
      cout << i;                                  /* the i is set to 0*/
      ........... some code
 
}

It is a good idea to use the character '\0' to test the end of line which has been saved in charbuff as this:

I get the charbuff as cin.getline(charbuff, 50); and read untill end of line

while(charbuff[i]=='\0')
{.........

or it is a better way?

use != not the == operator. But yes, C style strings always should be terminated with '\0' null terminator.

while(charbuff[i] !='\0')
{.........

or it is a better way?

depends on what you want to do.

> while(charbuff!=' ')
Maybe this is false on the first iteration, therefore i never gets modified and the function exits without doing anything.

> ........... some code
Post COMPLETE whole programs which show the problem.
Most efforts to snip out "irrelevant" code usually end up snipping out something really vital to the question at hand. This seems to be no exception.

I was working on a prog. which reads in a line from a console and saves the sentences and the words in a different files.
It works now fine.
I will use this files. ( I found out that I will need make some another functions which takes out special characters like . , ; and so on from this files. So I go work on it)

char charbuff[50];
string runlevel;


	int saveword ()
	{
	int i=0;
	ofstream WordsStream("words.lng", ios::app);
		while(charbuff[i]!=' ')
		{
		WordsStream << '*';
			while(charbuff[i]!=' ')
                	{
				if(charbuff[i]=='\0')
				{
				return 0;
				}
			WordsStream << charbuff[i];
                	i++;
			}
			if(charbuff[i]==' ')
			{
			i++;
			}
		}
	WordsStream.close();
	return 0;
	}

	int savesent()
	{	
	int i=0;
	ofstream SentStream("sentences.lng", ios::app);
	SentStream << '*';
		while(charbuff[i]!='\0')
                {
		SentStream << charbuff[i];
		i++;
		}
	SentStream << endl;
	SentStream.close();
	return 0;	
	}

	int readin()
	{
	cin.getline(charbuff,50);
	savesent();
	saveword();
	return 0;
	}

	int standby()
	{
		while(runlevel=="awake")
		{
		readin();
		}
	cout << "program terminated" << endl;
	return 0;
	}

int main()
{
cout << "heone" << endl;
runlevel="awake";
standby();
return 0;
}

thanks

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.