output from loop and writing to file (seperate problems)

This is a discussion on output from loop and writing to file (seperate problems) within the C++ Programming forums, part of the General Programming Boards category; lol, I'm back.... I'll probably get it fixed on my own, but still.... maybe someone else will have the same ...

  1. #1
    Registered User
    Join Date
    Aug 2004
    Posts
    13

    Question output from loop and writing to file (seperate problems)

    lol, I'm back.... I'll probably get it fixed on my own, but still.... maybe someone else will have the same problem and can figure it out from this instead of having to make a post....
    Code:
    	case 'b':	//pulls list of bowlers, creates score file for bowlers, adds scores to file
    		{
    			if (caseb == 1)
    				goto LOAD;
    			else
    				goto STARTB;
    
    STARTB:							//pulls list of bowlers
    
    			value = 0;
    			MasterlistIn.open("Masterlist.txt");
    			getline(MasterlistIn, Master[value]);
    			value++;
    			while (MasterlistIn)
    			{
    				getline(MasterlistIn, Master[value]);
    				value++;
    			}
    			control = value;
    			MasterlistIn.close();
    			caseb++;
    
    LOAD:							//creates/opens bowler score file and adds scores
    
    			cout << "Select created bowler." << endl << endl;
    			control = value;
    			value = 0;
    			for (value = 0; value < control - 1; value++)
    			{
    				cout << "Press " << value + 1 << " to load " << Master[value] << "." << endl;
    			}
    			cin >> loadbowl;
    			system("cls");
    			cout << "Loading bowler data for " << Master[loadbowl - 1] << endl;
    			bowler = Master[loadbowl - 1] + "_scores.txt";
    			inData[loadbowl - 1].open(bowler.c_str());
    			inData[loadbowl - 1].close();
    			outData[loadbowl - 1].open(bowler.c_str(), ios::app);
    			Sleep(1000);
    			system("cls");
    			cout << Master[loadbowl - 1] << " loaded." << endl;
    			system("cls");
    			cout << "Enter scores (3 games!)." << endl << endl
    				 << "Score for first game." << endl;
    			cin >> game1;
    			cout << endl
    				 << "Score for second game." << endl;
    			cin >> game2;
    			cout << endl
    				 << "Score for third game." << endl;
    			cin >> game3;
    			set = game1 + game2 + game3;
    			cout << "Total set is " << set << " ." << endl;
    			outData[loadbowl - 1] << game1 << endl
    								  << game2 << endl
    								  << game3 << endl
    								  << set << endl;
    			Sleep(1000);
    			outData[loadbowl - 1].close();
    		} break;
    ok, first problem, the loop, fixed my initial problem which was after returning to the menu and choosing this option, lol, maybe I should explain what the code does.... Ok, this part, it opens the master list from my previous thread and pulls out all the names.... shows the list on screen, the user selects a name from the list, the program creates a file for the name and then lets the user input the scores for that name.... it adds the scores together and gives that total.... program gives th eoption to exit or return to menu....

    now for the problems.... my initial problem I was talking about earlier, you run this option, return to the menu and select this option again.... The list of names doesn't show, but you can type in the number for one of the names, and it's recognized.... I fixed that by the if-goto-else-goto and the caseb++ up at the top.... But each time you run this option without exiting, the last name on the list doesn't show.... I'm testing it with 4 names, the first time, all four show, the second time, only the first 3 show, the 3rd time, only the first two show.... I'm sure if I add control++ after the control = value right inside of LOAD, it'll fix it, but I'm not sure.... might have to setup an if statement and put a control in there like I did with the caseb++ and the if-goto at the top....

    next problem.... it won't copy the scores and total set into the file for the very first name on the list.... it works fine for all of the other names, just the first on the list is giving me problems....

  2. #2
    Registered User
    Join Date
    Aug 2004
    Posts
    13
    ok.... weird.... didn't change anything, it'll write the scores to the file for the first name on the list now.... any idea why it'd do that?

  3. #3
    Registered User
    Join Date
    Aug 2004
    Posts
    13
    the control++ right after the control = value inside of LOAD I was talking about keeps the last name from disappearing, but it adds another line.... Shows the four names and a fifth without a name, doens't add a sixth or seventh with each repetition and it doesn't take away the last name, either.... any way around this?

  4. #4
    Teenage Mutant Ninja Nerd MMD_Lynx's Avatar
    Join Date
    Aug 2004
    Posts
    65
    you generally don't want to use gotos. try if else statements instead. sometimes i wonder why books teach you goto.
    Stupid people are useful. You can make them do all the mindless tasks you are too lazy to do yourself.

    Sphynx cats are just bald and wrinkly, like old people, and we don't reject them.

  5. #5
    Anal comment spacer DominicTrix's Avatar
    Join Date
    Apr 2002
    Posts
    120
    in your first "for" loop (the LOAD section), you have:

    Code:
    for (value = 0; value < control - 1; value++)
    {...
    I would have thought you'd want:

    Code:
    for (value = 0; value < control; value++)
    {...
    as when looping through an array, the code
    Code:
    for (value = 0; value <  arraysize; value++)
    {...
    is correct as < arraysize will make it stop at arraysize-1, which is the index of the last item in the array.

    You probably didn't need that explaing but hey!

    Hope that helps,

    dt
    "The most important thing about acting is honesty. If you can fake that you've got it made" - George Burns

  6. #6
    Registered User
    Join Date
    Aug 2004
    Posts
    13
    trying something.... noticed I was using control up in the menu section, that's why the list would disappear entirely the second time around.... with value < control - 1, it drops the last name with each repetition unless I put in a control++ before the loop, which then caused it to print out 5 lines instead of the 4 because there's 4 names in the test file.... Each repetition prints 5 lines, so.... I'm changing all of the "control"s in the second option to index and trying value < index in the loop....

    value < index prints out 5 lines.... value < index - 1 works perfectly.... this makes no sense.... honestly, it doesn't....

  7. #7
    Registered User
    Join Date
    Aug 2004
    Posts
    13
    halfway there.... just gotta work on the sections to find high game and high set and calculate averages.... that won't be difficult once I remember how to sort lists....

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Formatting a text file...
    By dagorsul in forum C Programming
    Replies: 12
    Last Post: 05-02-2008, 04:53 AM
  2. Formatting the contents of a text file
    By dagorsul in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2008, 01:36 PM
  3. Batch file programming
    By year2038bug in forum Tech Board
    Replies: 10
    Last Post: 09-05-2005, 04:30 PM
  4. Encryption program
    By zeiffelz in forum C Programming
    Replies: 1
    Last Post: 06-15-2005, 04:39 AM
  5. Replies: 3
    Last Post: 03-04-2005, 02:46 PM

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