Input File HELP, weird problem

This is a discussion on Input File HELP, weird problem within the C++ Programming forums, part of the General Programming Boards category; Hi all, I'm writing a program for school and I am having a weird problem. It's a menu driven program ...

  1. #1
    Still Learning
    Join Date
    Mar 2005
    Location
    CT/MA
    Posts
    5

    Question Input File HELP, weird problem

    Hi all,

    I'm writing a program for school and I am having a weird problem. It's a menu driven program which allows the user make choices, do what is involved in the choice, then return to the main menu and make another choice. Each choice coincides with opening a certain input file and reading into some global variables.

    My problem is when I run any choice from scratch, i.e. it's the first choice, the program runs fine, when I come back to the main menu and make another selection, the input file does not get read correctly. I have attached the code that is relevant to my problem. If anyone can help me I would really appreciate it. If you need more explanation or more info I'd be happy to put it up.

    I've checked to see if the file does not get opened properly and also if there is a read error, but there is neither. When I print out the variables after the second choice is made all the variables remain the same accept that the "alph" array is empty. I can't make heads or tails of it. It just seems that the variables are not being touched the second time around.

    In a nutshell, the program should prompt the user for a choice, open the file corresponding to the choice and read in the variables, then perform the checkstr() function. When returning from checkstr() it will break from the switch and then theoretically start over since it's do_while(true). The theoretically is where the problem is.

    Thanks in advance.

    Code:
    #include <windows.h>
    #include <conio.h>
    #include <iostream>
    #include <iomanip>
    #include <fstream>
    #include <string>
    
    using namespace std;
    
    fstream my_input_file;
    
    int menu_ch,
    	start_st,
    	no_fin_sts,
    	fin_sts[200],
    	no_rows,
    	no_cols,
    	tran_table[200][200],
    	cur_st,
    	nex_st,
    	input_exh,
    	inptr,
    	alph_len,
    	k;
    
    string file_path;
    
    char alph[200],
    	 in_str[1000];
    
    void display_menu();
    void open_file();
    void check_str();
    void trace_disp();
    
    int main(void)
    {
    
    	//HANDLE hConsole = GetStdHandle ( STD_OUTPUT_HANDLE );
    
    	do
    	{
    		system("cls");
    
    		display_menu();
    
    		cin >> menu_ch;
    		
    		switch(menu_ch)
    		{
    			case 1:
    				file_path = "notripcd.txt";
    				open_file();
    				check_str();
    				break;
    
    			case 2:
    				file_path = "decdiv25.txt";
    				open_file();
    				check_str();
    				break;
    
    			case 3:
    				file_path = "genome.txt";
    				open_file();
    				check_str();
    				break;
    	
    			case 4:
    				cout << "\nPlease enter the path of your file: ";
    				cin  >> file_path;
    				open_file();
    				check_str();
    				break;
    
    			case 5:
    				exit(0);
    
    			default:
    				cout << "\n\tInvalid entry please try again!!\n\n\t";
    				system("pause");
    		}
    
    	}while(true);
    
    	return 0;
    
    }
    
    void open_file()
    {
    	my_input_file.open(file_path.c_str(), ios::in);
    
    	if(my_input_file.bad())
    	{
    		cout << "Error reading file!";
    		exit(0);
    	}
    
    	my_input_file >> start_st >> no_fin_sts;
    
    	for(int i=0;i<no_fin_sts;i++)
    		my_input_file >> fin_sts[i];
    
    	my_input_file >> alph >> no_rows >> no_cols;
    	
    	for(i=0;i<no_rows;i++)
    	{
    		for(int j=0;j<no_cols;j++)
    			my_input_file >> tran_table[i][j];
    	}
    
    	my_input_file.close();
    
    }

  2. #2
    Still Learning
    Join Date
    Mar 2005
    Location
    CT/MA
    Posts
    5
    oh yah,

    if anyone knows how to change the console size within the program that's a little tweak I'm trying to work in as well. You might have noticed the "conio.h" and "HANDLE hConsole = GetStdHandle ( STD_OUTPUT_HANDLE );" that was commented out. I'm not really sure how to work with it but if someone knows an easy way to enlarge the console that would be cool.

    Thanks again

  3. #3
    Rabite SirCrono6's Avatar
    Join Date
    Nov 2003
    Location
    California, US
    Posts
    269
    As I am pondering the problem with your code, you can read about resizing the console at adrianxs's site.

    - SirCrono6

    Edit: Have you tried printing the variable after it is input to check if it is being read?
    Last edited by SirCrono6; 03-22-2005 at 08:33 PM.
    From C to shining C++!

    Great graphics, sounds, algorithms, AI, pathfinding, visual effects, cutscenes, etc., etc. do NOT make a good game.
    - Bubba

    IDE and Compiler - Code::Blocks with MinGW
    Operating System - Windows XP Professional x64 Edition

  4. #4
    Still Learning
    Join Date
    Mar 2005
    Location
    CT/MA
    Posts
    5
    SirCrono6,

    I was printing the variables that were being read and I also printed the "file_path" variable to make sure the correct file was being opened. After the first choice is made the variables print correctly. On the second choice they should all be different but they all print exactly the same accept for the "alph" variable which is the only string variable being read from the file. Thanks for looking into it, if you need examples of input files or any other info, please let me know.

    Thanks again

  5. #5
    Still Learning
    Join Date
    Mar 2005
    Location
    CT/MA
    Posts
    5
    SirCrono6,

    Adrianxs's site was extremely helpful, thanks for recommending it, not only was I able to adjust the console size but add a little color and a title to the window as well. If this file reading problem is solved my program will be complete.

    Thanks again, great recommendation

  6. #6
    Still Learning
    Join Date
    Mar 2005
    Location
    CT/MA
    Posts
    5
    Hi all,

    I am just checking to see if anyone has any feedback on this issue. I still can't figure out why the input file is not reading correctly the second time around.

    Thanks again.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem with Creating File for File Processing
    By Dampecram in forum C Programming
    Replies: 2
    Last Post: 12-07-2008, 12:26 AM
  2. problem collecting data from input file
    By gkoenig in forum C Programming
    Replies: 12
    Last Post: 03-30-2008, 07:40 AM
  3. gcc link external library
    By spank in forum C Programming
    Replies: 6
    Last Post: 08-08-2007, 03:44 PM
  4. input file problem
    By dudinka in forum C Programming
    Replies: 3
    Last Post: 05-07-2005, 08:55 AM
  5. Problem with File Input & Pointers
    By jenna in forum C++ Programming
    Replies: 9
    Last Post: 12-04-2004, 10:34 PM

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