Problems in forever loop

This is a discussion on Problems in forever loop within the C Programming forums, part of the General Programming Boards category; i made a multidimensional title saver in forever loop but it doesnt work it outputs weird title name i made ...

  1. #1
    Registered User
    Join Date
    Dec 2008
    Posts
    183

    Problems in forever loop

    i made a multidimensional title saver in forever loop but it doesnt work it outputs weird title name i made it work with normal forloop but i also wanna understand like how to make it work with a forever loop too thanks
    Code:
    #include <stdio.h>
    #include <ctype.h>
    int main(void)
    {
    	char title[60][100];
    	char result;
    	int counter;
    	counter=0;
    
    	for(;;)
    {
    	fputs("Do you want to continue or not ? (Y/N): ",stdout);
    	scanf("%c",&result);
    	result=toupper(result);
    	getchar();
    if(result=='N')
    {
        break;
    }
    	        printf("Please Enter your name: ");
    			getchar();
             	fgets(title[counter],60,stdin);
    	/*here to increase our counter*/
    	if(++counter==100)//notice increment happens before the comprasion
    	{
    	break;
    	}
    	printf("The details of CD %d is \n",counter);
    	printf("The detail of Title is : %s",title[counter]);
    }
    	return 0;
    }

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,636
    For starters, indent your code properly. What do you mean by "it doesnt work it outputs weird title name"?
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    Code:
    fgets(title[counter],60,stdin);
    	/*here to increase our counter*/
    	if(++counter==100)//notice increment happens before the comprasion
    	{
    	break;
    	}
    You want to stop at 59, not 99. The first dimension of your array only has 60 elements (of 100 characters). Which also means your fgets() is wrong.

    See the FAQ for a more reliable way to flush stdin.

  4. #4
    Registered User
    Join Date
    Dec 2008
    Posts
    183
    it outputs like [[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[
    instead of full name why would i stop at 59 instead of a 100 ? its a name of 60 char thats 100 titles
    i made it with normal for loop and it worked
    Code:
    #include <stdio.h>
    #include <ctype.h>
    int main(void)
    {
    	char title[60][100];
    	char result;
    	int counter;
    for(counter=0;counter<=100;counter++)
    {
    	fputs("Have you anymore cd to enter? (y/n)",stdout);
    	scanf("%c",&result);
    	getchar();
        result=toupper(result);
    	if(result!='Y')
    	{
    	break;
    	}
    	fputs("Please enter your name: ",stdout);
    	fgets(title[counter],60,stdin);
    	fputs("Press enter to see the next character of the title saver: ",stdout);
    	getchar();
        /*output cd details*/
    	printf("the title number <<%d>> : %s",counter+1,title[counter]);
    }return 0;}
    but i understand forever loops more.

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,636
    Quote Originally Posted by lolguy
    instead of full name why would i stop at 59 instead of a 100 ? its a name of 60 char thats 100 titles
    Actually, title is an array of 60 char arrays, each of 100 chars. Your loop loops over each element of title, not each char in each element of title.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,302
    Quote Originally Posted by laserlight View Post
    Actually, title is an array of 60 char arrays, each of 100 chars. Your loop loops over each element of title, not each char in each element of title.
    Or in layman's terms, you got the numbers round the wrong way.
    You wanted:
    Code:
    	char title[100][60];
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  7. #7
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    And it appears to be a repeated problem:
    http://cboard.cprogramming.com/showt...298#post820298

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 8
    Last Post: 12-01-2008, 09:09 AM
  2. A somewhat bizzare problem!!! - WHILE LOOP
    By bobthebullet990 in forum C Programming
    Replies: 3
    Last Post: 03-31-2006, 06:19 AM
  3. "for" loop problems....
    By hayai32 in forum C Programming
    Replies: 4
    Last Post: 05-04-2005, 01:20 AM
  4. Scope And Parameter Passing
    By djwicks in forum C Programming
    Replies: 6
    Last Post: 03-28-2005, 07:26 PM
  5. problems with greatest to least sorting
    By Mr_Jack in forum C++ Programming
    Replies: 2
    Last Post: 03-11-2004, 09:12 PM

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