tags:

views:

342

answers:

6

It's a c++ exercise, I will copy and paste it below with the code that I came up with. The code seems to be getting stuck in a loop and the texts scrolls across my screen so fast that I have no idea what to do and its hard to stop it. I have no idea where I'm going wrong so if someone could look at it for me please I'd be really greatful.

This is the exercise

(1) set up a league table with the number of teams (say 4 for simplicity) set up the struct league set up a dynamic array of numTeams for the league write a function to enter all the team names and set all the other variables to 0

(2) set up a composite struct called e.g. TeamsPlayedOrNot set up an array of size numTeams you will enter each team and the corresponding information for the teams they will play and whether they have played or not. This team information will be got from the league table teams. You should try to do this automatically. Set playedOrNot to false This information will be used to decide what teams have played against each other. See part 3

(3) Its now time to play a match!!!!!!! Look up a main team and a team they must play against. Ensure that a team hasn’t played a match. You can print out the teams that hav’nt played for the given team and the position in the inner array. You need to set playedOrNot to true.

Collect the information required for the league match and update these details into the league for each corresponding teams. Also enter these details into the TeamsPlayedOrNot array for the second team

(4) It is now time to sort the league table in order from the highest score to the lowest score. You can use a selection sort/bubble sort- some simple sort

and the following link is a PasteBay link to the code that I have prepared.

http://pastebay.com/72378

I hope you can help, thanks.

+4  A: 

Set some breakpoints in your code (or some console input statements) so that you can see what is happening each time through the loop.

Robert Harvey
A: 

The code seems to be getting stuck in a loop and the texts scrolls across my screen so fast that I have no idea what to do and its hard to stop it.

If you're using the program output (instead of a debugger) to debug it, then either make your screen buffer bigger (so that you can scroll what was output to your screen), or redirect the standard output to a file (whose contents you can then scroll, using a text editor).

ChrisW
+2  A: 

The easiest way to trap the code during a debugging session is this:

int loopy = 1;
// Somewhere in the code before the meat of the execution begins
while (loopy);

Now that the code will go into an infinite loop, look for the process id of that program executable and load it into the debugger (make sure debugging symbols are set on!). Set the breakpoint on the line indicating

while (loopy) ; 
and continue execution under debugger control, when the debugger hits on the breakpoint, set the value of loopy to 0 for the while loop to break! Now you can proceed to step through it and closely monitor what is happening.

Hope this helps you and eases your debugging a bit, Best regards, Tom.

tommieb75
This technique is used on the command line and have used it extensively under AIX at my previous job.
tommieb75
+1  A: 
  1. Rename your variables so that the names are useful. For example, you have an array "T". "T" is not descriptive of how the variable is used. If it's an array of teams, name it "teams".
  2. If you want help identifying what is failing, it's useful to provide the steps you take to reproduce the problem and the output you see. Exaclty what do you do - tell us every key that you touch - to cause the erronious behaviour. Does the problem occur immediately after starting the app, or do you enter a single option, then the error occurs?
  3. You see text running across your screen - it sounds like the application is in an infinite loop, reprinting the same section of text over and over. What section of text is it? You should be able to press CTRL+C to kill the program while it is running, and review the text being ouput.
  4. Line 51 should be a more descriptive prompt, such as "Enter the name of the new team: ". Anyone who doesn't know how the code reads won't know what to do. In general, when you want the user to do something, you should explicitly indicate what the user is supposed to do.
  5. line 55 probably does not do what you think it does. You're setting the character at newTeam[strlen(newTeam)] to '\0';. This occurs immediately after reading a string into the newTeam buffer. By definition, a string ends with the null character, so this line is redundant. What are you trying to accomplish?
atk
A: 

As others have said, it's hard to say what might be the problem without you reducing the code. I would guess that it would be something happening in your loop beginning

while (menuChoice != 4)

Maybe the code is choking when you read values into menuChoice in this line?

cin >> menuChoice;

Try replacing that with

menuChoice = 1;

to begin with, and see where that gets you.

Shoko
+1  A: 

It is always useful to compile with the -Wall option. This enables "warnings all". Useful when looking for coding mistakes. Your code has some warnings. Your code should compile with NO warnings.

so> g++ -g -Wall -o team team.cpp 
team.cpp: In function ‘int main()’:
team.cpp:54: warning: right-hand operand of comma has no effect
team.cpp:54: warning: right-hand operand of comma has no effect
team.cpp:71: warning: statement has no effect
team.cpp:73: warning: right-hand operand of comma has no effect
team.cpp:73: warning: right-hand operand of comma has no effect
team.cpp:73: warning: right-hand operand of comma has no effect
team.cpp:73: warning: right-hand operand of comma has no effect
team.cpp:73: warning: right-hand operand of comma has no effect
team.cpp:73: warning: right-hand operand of comma has no effect
team.cpp:73: warning: right-hand operand of comma has no effect
team.cpp: In function ‘void calculateResult(T*, int, int, int, int)’:
team.cpp:136: warning: unused variable ‘numOfTeams’
team.cpp: In function ‘void sortTable(T*)’:
team.cpp:141: warning: unused variable ‘numOfTeams’
so>

The -g option on the command line enables debugging capabilities, so you can use debuggers to step through your code. If your system has a compiler it probably has gdb. This is the GNU debugger. Here is a small run with gdb of your program:

so> gdb team
GNU gdb 6.3.50-20050815 (Apple version gdb-768) (Tue Oct  2 04:07:49 UTC 2007)
Copyright 2004 Free Software Foundation, Inc.
GDB is free software, covered by the GNU General Public License, and you are
welcome to change it and/or distribute copies of it under certain conditions.
Type "show copying" to see the conditions.
There is absolutely no warranty for GDB.  Type "show warranty" for details.
This GDB was configured as "i386-apple-darwin"...Reading symbols for shared libraries .... done

(gdb) break main
Breakpoint 1 at 0x26ba: file team.cpp, line 29.
(gdb) run
Starting program: /Users/dmcdon/Downloads/team 
Reading symbols for shared libraries +++. done

Breakpoint 1, main () at team.cpp:29
29   int menuChoice = 0;
(gdb) display menuChoice
1: menuChoice = 0
(gdb) step
30   int numOfTeams = 0;
1: menuChoice = 0
(gdb) 
36      while (menuChoice != 4)
1: menuChoice = 0
(gdb)

The -o option on the compile line says name the executable "team".

  1. "gdb team" launches the debugger for the executable "team".
  2. break main - sets a breakpoint at the main() function.
  3. run - starts program execution and runs up to the breakpoint you set at main.
  4. display menuChoice - tells the debugger to display menuChoice variable when it is in-scope.
  5. step - single steps through the code.
  6. hitting enter repeats the last command.

Useful gdb tips:

  • help - displays help in gdb
  • help list - shows help for the list command.
  • list - used to display your source code (you need that -g option at compile time).
  • display - to display an in-scope variable as it changes.
  • continue - continue execution until the next breakpoint
  • quit

Hope this helps.

DanM