looping problem

This is a discussion on looping problem within the C++ Programming forums, part of the General Programming Boards category; ok i think i have a problem with my loop in a function. i am reading a file to it ...

  1. #1
    Registered User
    Join Date
    May 2002
    Posts
    84

    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;
    }

  2. #2
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    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...
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

  3. #3
    Registered User
    Join Date
    May 2002
    Posts
    84
    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

  4. #4
    Registered User
    Join Date
    Sep 2004
    Posts
    719
    post your output, your expected output, and the flight schedule (only post about 5-10 lines though)
    i seem to have GCC 3.3.4
    But how do i start it?
    I dont have a menu for it or anything.

  5. #5
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    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.
    You're only born perfect.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C++ problem
    By sami_chy in forum C++ Programming
    Replies: 2
    Last Post: 07-24-2005, 12:59 AM
  2. Words and lines count problem
    By emo in forum C Programming
    Replies: 1
    Last Post: 07-12-2005, 04:36 PM
  3. half ADT (nested struct) problem...
    By CyC|OpS in forum C Programming
    Replies: 1
    Last Post: 10-26-2002, 09:37 AM
  4. binary tree problem - help needed
    By sanju in forum C Programming
    Replies: 4
    Last Post: 10-16-2002, 06:18 AM
  5. Looping problem
    By sketchit in forum C Programming
    Replies: 2
    Last Post: 10-01-2001, 03:19 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21