-
looping problem
ok i think i have a problem with my loop in a function. i am reading a file to it and i need it to check to see if the buffer is equal to the user input, and if not go to the next line and do the sane again and print out when the result is found
i think the problem is in the loop as its not working properly but not sure, can someone please help
Code:
// Cfunctions.cpp: implementation of the functions class.
//
//////////////////////////////////////////////////////////////////////
#include "functions.h"
#include <iostream>
#include <windows.h>
#include <fstream.h>
#include <conio.h>
#include <stdlib.h>
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
Cfunctions::Cfunctions()
{
}
Cfunctions::~Cfunctions()
{
}
Cfunctions::Printall()
{
system("cls"); //try to find an alternative in the faq
ifstream file;
file.open("flight.txt",ios::nocreate);
if(!file)
return -1;
while(file && !file.eof())
{
file.get(ch);
switch(ch)
{
case '~':
case '%':
case '#':
ch = 0;
break; //really kind of unecessary
}
cout << ch << flush;
}
file.close();
getch();
return 0;
}
int Cfunctions::Flightdetails()
{
char temp[5];
cout << "Please enter flight number" << endl;
cin >> temp;
system("cls");
ifstream is("flight.txt",ios::nocreate);
if(!is)
return -1;
char buffer[48];
char buffer2[48];
do
{
is.getline(buffer,48, '#');
if (buffer[0] == temp[0])
{
if (buffer[1] == temp[1])
{
if (buffer[2] == temp[2])
{
if (buffer[3] == temp[3])
{
if (buffer[4] == temp[4])
{
if (buffer[5] == temp[5])
{
cout << temp[0] << endl;
cout << temp[1] << endl;
cout << temp[2] << endl;
cout << temp[3] << endl;
cout << temp[4] << endl;
cout << temp[5] << endl;
cout << buffer << endl;
}
}
}
}
}
}
else
{
if (n == 0)
{
is.seekg(45, ios::beg);
n++;
cout << n << endl;
}
else
{
is.seekg(45, ios::cur);
}
}
}while(is && !is.eof());
is.close();
return 0;
}
-
first, I took a look at your headers... some of them are antiquated--you have:
Code:
#include "functions.h"
#include <iostream>
#include <windows.h>
#include <fstream.h>
#include <conio.h>
#include <stdlib.h>
and it should be:
Code:
#include "functions.h"
#include <iostream>
#include <windows.h>
#include <fstream>
#include <conio.h>
#include <cstdlib>
other than that, I haven't really looked over your code because you didn't include your class definition...
-
ok header file for the function class
Code:
// Cfunctions.h: interface for the functions class.
//
//////////////////////////////////////////////////////////////////////
#if !defined(AFX_FUNCTIONS_H__ECEF15DB_CE60_4219_8B22_357BD983EDFA__INCLUDED_)
#define AFX_FUNCTIONS_H__ECEF15DB_CE60_4219_8B22_357BD983EDFA__INCLUDED_
#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
class Cfunctions
{
public:
Cfunctions();
virtual ~Cfunctions();
int Printall();
int Flightdetails();
char ch;
char flightno[6];
int n;
};
and main function
Code:
#include <iostream>
#include <windows.h>
#include <fstream.h>
#include <conio.h>
#include <stdlib.h>
#include "functions.h"
Cfunctions func;
int numchoice;
int main()
{
cout << "Welcome to the Airport Flight Database" << endl;
cout << " " << endl;
cout << "1 - Display all Flight information" << endl;
cout << "2 - Display details for Flight number________ " << endl;
cout << "3 - Find Flight(s) for a set route" << endl;
cout << "4 - Display Flight Length" << endl;
cout << "5 - Flight Extra's" << endl;
cout << "6 - Exit Program" << endl;
cin >> numchoice;
switch(numchoice)
{
case 1:
func.Printall();
break;
case 2:
func.Flightdetails();
break;
case 6:
return 0;
break;
default:
cout << "Error invalid entery please try again" << endl;
break;
}
return 0;
}
prob same again with main for #includes
-
post your output, your expected output, and the flight schedule (only post about 5-10 lines though)
-
REmember, array indexes are 0 based. That means that temp will only hold 4 readable char and a null char given declaration of:
char temp[5];
Therefore trying to compare buffer[5] with temp[5] as in :
if (buffer[5] == temp[5])
won't work because temp[5] is garbage, doesn't exist, etc.
If the flight number is 5 digits/letters long, then temp should be declared with 6 elements so temp[5] will be the sixth char in the string, and will actually be the null char, with temp[0] through temp[4] being the readable char of the flight number. I would not try to compare temp[5] with buffer[5] either, because unless buffer.size() == 5, meaning getline() put a null char at buffer[5], then buffer[5] will never have a chance to equal temp[5], even then. I'd just compare temp[0] through temp[4] with buffer[0] through buffer[4] if flight number is 5 char long.