10 Replies - 596 Views - Last Post: 12 April 2010 - 07:34 AM Rate Topic: -----

#1 dunsta  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 79
  • Joined: 08-April 10

can't get out of the loop

Posted 09 April 2010 - 07:02 PM

I had this program working, but it would only cycle through once, then quit. So I wanted to make it loop to continue using until the user was ready to quit. This is when I added the bool/while/and finished statements. It runs and loops, but I have to perform a command window shutdown to get out of it.
One area of note is that when I added the "else if" I got an error message saying I needed a "(" before the printf statement, first time I have needed this.
I think it is a simple solution but its not coming to me.



#include <stdio.h>
#include <stdlib.h>
#include <string.h>


char userEntry[10];
char secUserEntry[10];

int main (int argc, const char * argv[]) 
{
    
	printf("First of all, enter a name for me to store as a string > ");
	scanf("%11s", userEntry);
	fpurge(stdin);
	
	printf("User entered : %s", userEntry); 

	printf("Now, enter a second name for me to compare with the first > ");
	scanf("%11s",secUserEntry);
	fpurge(stdin);
	
	if (strcmp(char * userEntry, char * secUserEntry) == 0)
	{
		printf("\nThe two are identical, %11s = %11s", userEntry, secUserEntry);
	}
	else 
	{
		printf("\nThe two are not idntical, %11s != %11s", userEntry, secUserEntry);
	}		
	
	return 0;
}



Is This A Good Question/Topic? 0
  • +

Replies To: can't get out of the loop

#2 PlasticineGuy  Icon User is offline

  • mov dword[esp+eax],0
  • member icon

Reputation: 281
  • View blog
  • Posts: 1,436
  • Joined: 03-January 10

Re: can't get out of the loop

Posted 09 April 2010 - 07:04 PM

strcmp(char * userEntry, char * secUserEntry)
That's a prototype definition. What you want is this:
strcmp(userEntry, secUserEntry)

Was This Post Helpful? 0
  • +
  • -

#3 IngeniousHax  Icon User is offline

  • |>|20-514<|{3|2

Reputation: 78
  • View blog
  • Posts: 1,358
  • Joined: 28-March 09

Re: can't get out of the loop

Posted 10 April 2010 - 12:32 AM

PlasticineGuy is correct on his remark... however if you wanna use pointers, declare the global variables as:
char *userEntry[ ];   // with the array size in the declaration =)
char *secUserEntry[ ];




and the answer to your question:
int main (int argc, const char * argv[]) 
{
   char uChoice = 'y';
    do{
        printf("First of all, enter a name for me to store as a string > ");
        scanf("%11s", userEntry);
        fpurge(stdin);
        
        printf("User entered : %s", userEntry); 

        printf("Now, enter a second name for me to compare with the first > ");
        scanf("%11s",secUserEntry);
        fpurge(stdin);
        
        if (strcmp(char * userEntry, char * secUserEntry) == 0)
        {
                printf("\nThe two are identical, %11s = %11s", userEntry, secUserEntry);
        }
        else 
        {
                printf("\nThe two are not idntical, %11s != %11s", userEntry, secUserEntry);
        }
      printf("Run again? (y/n): ");
      scanf("%c", choice);  
      choice = toupper(choice);        
     }while(uChoice == 'Y');   
        return 0;
}


This post has been edited by IngeniousHax: 10 April 2010 - 12:32 AM

Was This Post Helpful? 1
  • +
  • -

#4 dunsta  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 79
  • Joined: 08-April 10

Re: can't get out of the loop

Posted 10 April 2010 - 02:56 AM

I'm such a dummy, I had 2 windows open and posted the wrong code in my question above. Thanks for the answer IngeniousHax, but I'm quite new to C and haven't come across "uChoice" or "toupper" commands.

Anyway, here is the question and the code it relates to, unfortunately I posted the wrong one in my first post, I'm not called a DIC for nothing!

I had this program working, but it would only cycle through once, then quit. So I wanted to make it loop to continue using until the user was ready to quit. This is when I added the bool/while/and finished statements. It runs and loops, but I have to perform a command window shutdown to get out of it.
One area of note is that when I added the "else if" I got an error message saying I needed a "(" before the printf statement, first time I have needed this.
I think it is a simple solution but its not coming to me.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>


char userEntry[10];
char secUserEntry[10];
//bool finished;

int main (int argc, const char * argv[]) 
{
	bool finished;
	finished = false;
	while (finished != true){
    
	printf("First of all, enter a name for me to store as a string >\n ");
	scanf("%11s", userEntry);
	fpurge(stdin);
	
	printf("User entered : %s\n", userEntry); 
	
	printf("Now, enter a second name for me to compare with the first >\n ");
	scanf("%11s",secUserEntry);
	fpurge(stdin);
	 
	if (strcmp(/*char */ userEntry, /*char */ secUserEntry) == 0)
	
	printf("\nThe two are identical, %11s = %11s\n", userEntry, secUserEntry);
	
	else if
	
	(printf("\nThe two are not idntical, %11s != %11s \n", userEntry, secUserEntry));
	
		else 
			finished = true;
	}
	
	return 0;
}



hope it's not too late to get some help on this...thanks!
Was This Post Helpful? 0
  • +
  • -

#5 sarmanu  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 966
  • View blog
  • Posts: 2,362
  • Joined: 04-December 09

Re: can't get out of the loop

Posted 10 April 2010 - 03:28 AM

I have arranged your indentation a bit, and it came up with this:
if (strcmp(/*char */ userEntry, /*char */ secUserEntry) == 0)
    printf("\nThe two are identical, %11s = %11s\n", userEntry, secUserEntry);
else if (printf("\nThe two are not idntical, %11s != %11s \n", userEntry, secUserEntry));
else 
    finished = true;


So, basically, the first "if" checks if the strings are equal. The second if makes no sense, because it checks if "printf" can print something. Of course that this is gonna be always true, since this function can't really fail.
And why declare "userEntry" and "secUserEntry" global? Here's how I'd do it:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>

int main (int argc, const char * argv[]) 
{
    char userEntry[10];
    char secUserEntry[10];
    bool finished;
    finished = false;

    char choice, c;
    while (finished != true)
    {
	printf("First of all, enter a name for me to store as a string >\n ");
        scanf("%11s", userEntry);
        
        printf("User entered : %s\n", userEntry); 
      
        printf("Now, enter a second name for me to compare with the first >\n ");
        scanf("%11s",secUserEntry);
         
        // Just compare the arrays:
        if (strcmp(/*char */ userEntry, /*char */ secUserEntry) == 0)
	    printf("\nThe two are identical, %11s = %11s\n", userEntry, secUserEntry);
        else 
	    printf("\nThe two are not idntical, %11s != %11s \n", userEntry, secUserEntry);

        // This function clears the buffer, it's a good alternative for fflush
	while (( c = getchar()) != EOF && c != '\n');
        
        // Ask the user if he wants to continue, or to exit
	printf("Again? [Y/N] ");
	scanf("%c", &choice);

        // If the choice is not 'y' or 'Y' (stands for YES), then set finished to true
        // and the loop will stop
	if (choice != 'y' && choice != 'Y')
	    finished = true;
     }
        
    return 0;
}


This post has been edited by sarmanu: 10 April 2010 - 03:30 AM

Was This Post Helpful? 1
  • +
  • -

#6 dunsta  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 79
  • Joined: 08-April 10

Re: can't get out of the loop

Posted 10 April 2010 - 04:13 AM

thanks Sarmanu
Was This Post Helpful? 0
  • +
  • -

#7 PlasticineGuy  Icon User is offline

  • mov dword[esp+eax],0
  • member icon

Reputation: 281
  • View blog
  • Posts: 1,436
  • Joined: 03-January 10

Re: can't get out of the loop

Posted 10 April 2010 - 06:36 PM

View PostIngeniousHax, on 10 April 2010 - 04:32 PM, said:

PlasticineGuy is correct on his remark... however if you wanna use pointers, declare the global variables as:
char *userEntry[ ];   // with the array size in the declaration =)
char *secUserEntry[ ];

To quote Inglourious Basterds: NEIN NEIN NEIN NEIN NEIN NEIN NEIN!
A character array is a pointer with static memory instead of dynamic memory. Either use char* and malloc/string constants or arrays. Don't use both unless what you actually want is a pointer to an array of characters.
Was This Post Helpful? 0
  • +
  • -

#8 dunsta  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 79
  • Joined: 08-April 10

Re: can't get out of the loop

Posted 11 April 2010 - 11:09 PM

Yes P.Guy, I got the loop part of this programme working, to do so as you can see in the code for post #4, I took out the "char *" in the "strcmp" (what does that stand for anyway? string compile? ) and it runs with no errors.
 strcmp(/*char */ userEntry, /*char */ secUserEntry) == 0)



so does this mean that this code was using char pointers with no malloc, and arrays, before I took out the char* in strcmp.
And after I took out the char* in strcmp it is only using dynamic mem?

ps. I know I need to do a lot more work on this area before moving on.
Was This Post Helpful? 0
  • +
  • -

#9 PlasticineGuy  Icon User is offline

  • mov dword[esp+eax],0
  • member icon

Reputation: 281
  • View blog
  • Posts: 1,436
  • Joined: 03-January 10

Re: can't get out of the loop

Posted 12 April 2010 - 01:18 AM

int strcmp(char* str1, char* str2);
This is a prototype - a forward declaration of a function you define later.

Quote

so does this mean that this code was using char pointers with no malloc, and arrays, before I took out the char* in strcmp.
No, it means it was invalid C before you took out char*.

Quote

And after I took out the char* in strcmp it is only using dynamic mem?
It's still using static memory. It's dynamic only if you use malloc() and free().
Was This Post Helpful? 1
  • +
  • -

#10 dunsta  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 79
  • Joined: 08-April 10

Re: can't get out of the loop

Posted 12 April 2010 - 06:07 AM

Ok, string compare, ofcoarse. I've got a long way to go before I could even call myself a noob around here.
Thanks P.Guy
Was This Post Helpful? 0
  • +
  • -

#11 janotte  Icon User is offline

  • code > sword
  • member icon

Reputation: 990
  • View blog
  • Posts: 5,141
  • Joined: 28-September 06

Re: can't get out of the loop

Posted 12 April 2010 - 07:34 AM

View Postsarmanu, on 10 April 2010 - 02:28 AM, said:

The second if makes no sense, because it checks if "printf" can print something. Of course that this is gonna be always true, since this function can't really fail.


While 'fail' is an *interesting* term to use here printf() can return 0 which will resolve to false for an if test.
Read more here.
http://www.coders202...s-printf-return

While I am confident that the code was not what the OP intended, so sarmanu's point was good, checking the return value of printf() is one of the things that we all should do regularly if we are going to consider ourselves good defensive programmers.
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1