when i running this program, i've found that one of the used integer always over load.
that is int outq.

could someone please help me find out what's make this integer over load at last? thanks.

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <fstream>
#include <iostream>
#include <string.h>
#include <cctype>

using namespace std;
using std::string;

int main(void)
{
	FILE * finput;
	FILE * fcommand;
	FILE * creafile;

	for(int w=1; w<=2; w++){

	typedef struct pac{
	char strinput[40];
	};
	typedef struct com{
	char strcommand[25];
	};

	int noofline;
	int q = 0;
	int outq = 0;

	if(w==1) {noofline = 10; finput = fopen("input1.txt","r"); fcommand = fopen("command1.txt","r"); creafile = fopen("result1.txt","w");}
	else {noofline = 20; finput = fopen("input2.txt","r"); fcommand = fopen("command2.txt","r"); creafile = fopen("result2.txt","w");}

	struct pac packe[20];
	struct com comm[20];

	for(int i=0; i<noofline; i++)
	{for(int j=0; j<40; j++) packe[i].strinput[j]='0';
	for(int j=0; j<25; j++) comm[i].strcommand[j]='0';}

	int timeslot1[noofline];
	int timeslot2[noofline];

	for(int i=0; i<noofline;){

		fscanf(finput,"%d", &timeslot1[i]);
		fgets(packe[i].strinput, 40, finput);

		fscanf(fcommand,"%d", &timeslot2[i]);
		fgets(comm[i].strcommand, 25, fcommand);

		if(packe[i].strinput[5] == '8') q++;
		cout<<timeslot1[i]<<packe[i].strinput<<endl<<comm[i].strcommand<<endl;

		if(comm[i].strcommand[8] == 't'){
			fprintf(creafile, "%d %d \n", timeslot1[i], q);
			cout<<"the data in file : "<<timeslot1[i]<<"  "<<q<<"  ";
		}
		else if(comm[i].strcommand[1] == 'i'){
			fprintf(creafile, "%d \n", timeslot1[i]);
			cout<<"the data in file : "<<timeslot1[i]<<"  ";
		}
		else{
			bool b = true;
			while(b == true) {if(outq >=20) b = false; else if(packe[outq].strinput[21]!='0') outq++; else b = false;}
			fprintf(creafile, "%d %s", timeslot1[i], packe[outq].strinput);
			cout<<"the data in file : "<<timeslot1[i]<<"    "<<packe[outq].strinput;

			outq++;
			q=q-1;
		}
		cout<<comm[i].strcommand[1]<<"  "<<comm[i].strcommand[8]<<endl;

		cout<<"queue = "<<q<<"  outq = "<<outq;
		i++;
	}

	cout<<"Last queue no. is "<<q<<endl;

	fclose(finput);
	fclose(fcommand);
	fclose(creafile);

	cout<<"Press enter to continue...";
	cin.get();

	}
	return(0);
}

input1.txt

1 C0A80001 8F22 C0A80002 0050 32D5B9C
2 C0A80001 8B05 C0A80002 0050 12D67B
3 C0A80001 CC08 C0A80002 0050 32D5B9C
4
5
6 C0A80001 C4A3 C0A80002 0050 34F1
7 C0A80001 91BD C0A80002 0050 21E89A
8 C0A80001 D10A C0A80002 0050 32DCE1
9 C0A80001 AB4F C0A80002 0050 34EF
10

input2.txt

1 C0A80001 8F22 C0A80002 0050 32D5B9C
2 C0A80001 8B05 C0A80002 0050 12D67B
3 C0A80001 CC08 C0A80002 0050 32D5B9C
4
5
6 C0A80001 C4A3 C0A80002 0050 34F1
7 C0A80001 91BD C0A80002 0050 21E89A
8 C0A80001 D10A C0A80002 0050 32DCE1
9 C0A80001 AB4F C0A80002 0050 34EF
10
11 C0A80001 AC5F C0A80002 0050 35DCEB
12 C0A80001 B10C C0A80002 0050 42ABCD
13 C0A80001 C100 C0A80002 0050 3456AB
14 C0A80001 A1FF C0A80002 0050 1122BB
15
16
17
18 C0A80001 ABC5 C0A80002 0050 ABC123
19
20

command1.txt

1 output
2 output the queue length
3 idle
4 output
5 output
6 output the queue length
7 output
8 output
9 output
10 output

command2.txt

1 idle
2 output
3 output
4 idle
5 output the queue length
6 idle
7 output
8 output
9 output
10 idle
11 output the queue length
12 idle
13 idle
14 output
15 output the queue length
16 idle
17 idle
18 output
19 idle
20 output the queue length

sorry for inconvenient and thanks for patient.

lines 20 and 23 are wrong. Remove the word typedef from each of those two lines.

lines 31, 32, 64 and several others are just a huge mess. Put each of those statements that end with semicolons on different lines so that your program is easier to read. It not qute to jam all those statements on the same line and professional programmers rarely ever do that either.

I don't know what you mean by "integer overload". There is no such thing. The only places I see that variable are lines 29, 69 and 74. And I can't tell (due to horrible coding style) what it is that you are trying to do with it. A few comments is in order in your program.

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <fstream>
#include <iostream>
#include <string.h>
#include <cctype>

using namespace std;
using std::string;

int main(void)
{
	FILE * finput;
	FILE * fcommand;
	FILE * creafile;

	for(int w=1; w<=2; w++){

	struct pac{
	char strinput[40];
	};
	struct com{
	char strcommand[25];
	};

	int noofline;
	int q = 0;
	int outq = 0;

	if(w==1){//the case to read input1.txt command1.txt and write result1.txt, these 3 files got 10 lines.
		noofline = 10;
		finput = fopen("input1.txt","r");
		fcommand = fopen("command1.txt","r");
		creafile = fopen("result1.txt","w");
	}
	else{//the case to read input2.txt command2.txt and write result2.txt, these 3 files got 20 lines.
		noofline = 20;
		finput = fopen("input2.txt","r");
		fcommand = fopen("command2.txt","r");
		creafile = fopen("result2.txt","w");
	}

	struct pac packe[20];
	struct com comm[20];

	for(int i=0; i<noofline; i++)
	{for(int j=0; j<40; j++) packe[i].strinput[j]='0';
	for(int j=0; j<25; j++) comm[i].strcommand[j]='0';}

	int timeslot1[noofline];
	int timeslot2[noofline];

	for(int i=0; i<noofline;){

		fscanf(finput,"%d", &timeslot1[i]);
		fgets(packe[i].strinput, 40, finput);

		fscanf(fcommand,"%d", &timeslot2[i]);
		fgets(comm[i].strcommand, 25, fcommand);

		if(packe[i].strinput[5] == '8') q++;
		cout<<timeslot1[i]<<packe[i].strinput<<endl
			<<comm[i].strcommand<<endl;

		if(comm[i].strcommand[8] == 't'){
			fprintf(creafile, "%d %d \n", timeslot1[i], q);
			cout<<"the data in file : "<<timeslot1[i]<<"  "<<q<<"  ";
		}
		else if(comm[i].strcommand[1] == 'i'){
			fprintf(creafile, "%d \n", timeslot1[i]);
			cout<<"the data in file : "<<timeslot1[i]<<"  ";
		}
		else{
			bool b = true;
			while(b == true){
				if(outq >=20)//outq shouldn't over 20
					b = false;
				else if(packe[outq].strinput[21]!='0')//if the data in packe[outq].strinput[21] is 0 or not
					outq++;//cause if the command is 'output' then need to take out the previous data which in FIFO think
				else b = false;
			}
			fprintf(creafile, "%d %s", timeslot1[i], packe[outq].strinput);
			cout<<"the data in file : "<<timeslot1[i]<<"    "<<packe[outq].strinput;

			outq++;
			q=q-1;
		}
		cout<<comm[i].strcommand[1]<<"  "<<comm[i].strcommand[8]<<endl;



		cout<<"queue = "<<q<<"  outq = "<<outq;
//		cin.get();
		i++;
	}

	cout<<"Last queue no. is "<<q<<endl;

	fclose(finput);
	fclose(fcommand);
	fclose(creafile);

	cout<<"Press enter to continue...";
	cin.get();
	}
	return(0);
}
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.