LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 01-31-2023, 03:29 PM   #1
jmgibson1981
Member
 
Registered: Jun 2015
Location: Tucson, AZ USA
Distribution: Debian
Posts: 980

Rep: Reputation: 328Reputation: 328Reputation: 328Reputation: 328
For loop iterating endlessly - C


They've got me doing tic tac toe now. It works save the winner check. The for loop goes endlessly. I'm calling the for loop with all variables.

I've tested the variables are getting passed via printf statements. It just keeps looping till I stop it. Thank you in advance for any assistance.

Code:
void winner_check_launch(char gameboard[], char player)
{
    // horizontals
    winner_check(gameboard, player, 1, 3, 1);
    // verticals
    winner_check(gameboard, player, 1, 7, 3);
    // diagonals(
    winner_check(gameboard, player, 1, 9, 4);
    winner_check(gameboard, player, 3, 2, 7);
}

void winner_check(char gameboard[], char player, int ctr1, int ctr2, int ctr3) {
    int counter = 0;
    for (int i = ctr1; i <= ctr2; i + ctr3) {
        if (gameboard[i] == player) {
            counter += 1;
        }
    }
    if (counter == 3) {
        printf("%c wins! game over and thank you for playing\n", player);
        exit(0);
    }
}
Full source I'm on now. https://gitlab.com/jmgibson1981/mycp...0-%2068/main.c
 
Old 01-31-2023, 04:10 PM   #2
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,447
Blog Entries: 1

Rep: Reputation: 1687Reputation: 1687Reputation: 1687Reputation: 1687Reputation: 1687Reputation: 1687Reputation: 1687Reputation: 1687Reputation: 1687Reputation: 1687Reputation: 1687
Code:
Before: for (int i = ctr1; i <= ctr2; i + ctr3) {
After:  for (int i = ctr1; i <= ctr2; i += ctr3) {
 
Old 01-31-2023, 04:21 PM   #3
jmgibson1981
Member
 
Registered: Jun 2015
Location: Tucson, AZ USA
Distribution: Debian
Posts: 980

Original Poster
Rep: Reputation: 328Reputation: 328Reputation: 328Reputation: 328
Thank you so much. Always the small things. I was wondering why the compiler was saying "warning statement with no effect" (codeblocks).
 
Old 01-31-2023, 09:53 PM   #4
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian, Arch
Posts: 3,695

Rep: Reputation: 2025Reputation: 2025Reputation: 2025Reputation: 2025Reputation: 2025Reputation: 2025Reputation: 2025Reputation: 2025Reputation: 2025Reputation: 2025Reputation: 2025
By the way, I would also suggest some renaming, e.g.

Code:
for (int i = start; i <= end; i += step) {
 
1 members found this post helpful.
Old 02-01-2023, 09:37 AM   #5
jmgibson1981
Member
 
Registered: Jun 2015
Location: Tucson, AZ USA
Distribution: Debian
Posts: 980

Original Poster
Rep: Reputation: 328Reputation: 328Reputation: 328Reputation: 328
Good point. Thank you as well.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] For loop iterating over a list of IP addresses from a txt file hebruiser Programming 3 03-03-2018 10:33 PM
Firefox "*** loading the extensions datasource" endlessly Nekow42 Linux - Software 10 08-09-2005 04:02 PM
Flashing SL-5500 = LED lights flash endlessly Zeiph Linux - Laptop and Netbook 1 05-02-2005 06:15 AM
my computer crawls and is endlessly writing to hard drive lkrubner Fedora 4 01-24-2005 08:31 AM
CPU waits endlessly during disk transfers frostschutz Linux - Hardware 2 12-18-2004 12:52 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 06:06 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration