LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
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 10-11-2010, 12:27 AM   #61
boilers969
Member
 
Registered: Oct 2010
Posts: 55

Original Poster
Rep: Reputation: 0

Yes I'm understanding that. But since I can't have any global variables, where can I declare char shape??
 
Old 10-11-2010, 12:28 AM   #62
Nylex
LQ Addict
 
Registered: Jul 2003
Location: London, UK
Distribution: Slackware
Posts: 7,464

Rep: Reputation: Disabled
Wow, this thread has certainly grown overnight..

Quote:
Originally Posted by sag47 View Post
Code:
char shape=1;
shape='c';
switch(shape)
{
    case 'a':
        return 0;
    case 'b':
        return 0;
    default:
        return 1;
}
You can make that even more compact with

Code:
case 'a':
case 'b':
  return 0;
 
Old 10-11-2010, 12:34 AM   #63
graemef
Senior Member
 
Registered: Nov 2005
Location: Hanoi
Distribution: Fedora 13, Ubuntu 10.04
Posts: 2,379

Rep: Reputation: 148Reputation: 148
Quote:
Originally Posted by boilers969 View Post
Yes I'm understanding that. But since I can't have any global variables, where can I declare char shape??
shape will be declared within the main function. It will then be available to the switch statement which is in the main. Which will be within your while loop (as you asked about that earlier).

Now might be a good time to post your code as it stands so that we can have a good look at it and help you iron out any problems. Also include any compile errors or warnings that you may have.
 
Old 10-11-2010, 12:42 AM   #64
boilers969
Member
 
Registered: Oct 2010
Posts: 55

Original Poster
Rep: Reputation: 0
So are you saying the while loop will contain the switch statment?

Code:
#include<stdio.h>

int main()
{
 while(1)
        {
        char shape=1;                                                
        int row;                                                     
        printf("Enter shape type (s/h/f):");                        
        scanf("%c",&shape);                                          
        printf("Enter shape length: ");                              
        scanf("%d",&row);                                            
        if(row<=0)                                                   
        printf("Shape length cannot be negative. Try again\n");      
        }
char shape=1;
shape='s','h','f';
   switch(shape)
   {
        case s:
{
           int i,row;
           for(i=row;i>=1;i--)
                printf("*\n");
           break;
           }

        case h:
{
           int i,j,k,row,m;
           m=row;
           for(i=1 ; i<=m; i++)
           {
                for (j=1 ; j>=row; j--)
                   printf(" ");
{
                   for(k=1; k<=i; k=k+1)
                        printf("*");

                  printf("\n");
           }
        }
        break;
        }

        case f:
{
                int i,j,k,row,m;
                m=row;
                for(i=0;i<m;i++)
                {
                   printf("\n");
                   for(k=0;k<row;k++)
                        printf(" ");
                   for(j=0;j<=i;j++)
                        printf(" *");
                   row--;
                }
        }
}
printf("\n");
break;
}
}
}
return 0;
}
Errors:

shape.c: In function âmainâ:
shape.c:25: error: âsâ undeclared (first use in this function)
shape.c:25: error: (Each undeclared identifier is reported only once
shape.c:25: error: for each function it appears in.)
shape.c:33: error: âhâ undeclared (first use in this function)
shape.c:50: error: âfâ undeclared (first use in this function)
shape.c:66: error: break statement not within loop or switch
shape.c: At top level:
shape.c:68: error: expected identifier or â(â before â}â token
shape.c:69: error: expected identifier or â(â before â}â token
shape.c:70: error: expected identifier or â(â before âreturnâ
shape.c:71: error: expected identifier or â(â before â}â token
 
Old 10-11-2010, 12:46 AM   #65
sag47
Senior Member
 
Registered: Sep 2009
Location: Raleigh, NC
Distribution: Ubuntu, PopOS, Raspbian
Posts: 1,899
Blog Entries: 36

Rep: Reputation: 477Reputation: 477Reputation: 477Reputation: 477Reputation: 477
Yes. while(1) { } is an infinite loop. It will never get to char shape=1. Anything that you do with your code MUST be within the while loop if it is while(1).

So your code should look like
Code:
int main()
{
  while(1)
  {
    ...program about everything;
  }
}
There shouldn't be anything outside of while(1) below it. You can set variables and other conditions to be used within the while(1) above it however.

Also in order for your program to be successful in any way it has to exit some how. So somewhere in the while(1) you need to have a return statement.

Last edited by sag47; 10-11-2010 at 12:47 AM.
 
Old 10-11-2010, 12:49 AM   #66
boilers969
Member
 
Registered: Oct 2010
Posts: 55

Original Poster
Rep: Reputation: 0
Right!

Code:
#include<stdio.h>

int main()
{
 while(1)
        {
        char shape=1;                                                
        int row;                                                     
        printf("Enter shape type (s/h/f):");                        
        scanf("%c",&shape);                                          
        printf("Enter shape length: ");                              
        scanf("%d",&row);                                            
        if(row<=0)                                                   
        printf("Shape length cannot be negative. Try again\n");      

        char shape=1;
        shape='s','h','f';
   switch(shape)
   {
        case s:
{
           int i,row;
           for(i=row;i>=1;i--)
                printf("*\n");
           break;
           }

        case h:
{
           int i,j,k,row,m;
           m=row;
           for(i=1 ; i<=m; i++)
           {
                for (j=1 ; j>=row; j--)
                   printf(" ");
{
                   for(k=1; k<=i; k=k+1)
                        printf("*");

                  printf("\n");
           }
        }
        break;
        }

        case f:
{
                int i,j,k,row,m;
                m=row;
                for(i=0;i<m;i++)
                {
                   printf("\n");
                   for(k=0;k<row;k++)
                        printf(" ");
                   for(j=0;j<=i;j++)
                        printf(" *");
                   row--;
                }
        }
}
printf("\n");
break;
}
}
}
return 0;
}
How does that look? I might be off by a bracket somewhere.
 
Old 10-11-2010, 12:50 AM   #67
graemef
Senior Member
 
Registered: Nov 2005
Location: Hanoi
Distribution: Fedora 13, Ubuntu 10.04
Posts: 2,379

Rep: Reputation: 148Reputation: 148
Yes include the switch in the while loop
Code:
int main()
{
   while(1)
   {
      /* Code to get the value of shape */
      switch (shape)
      {
         case 's':
            /* code to handle the shape 's' */
            break
         /* the remaining case statements */
      } /* end of switch */
   } /* end of while loop forever */

} /* end of main function */
Note that the value held by shape be it a s h or f must be in single quotes 's', 'h' or 'f'

You don'd need to declare the variable a second time (I guess that you put it there because the compiler complained that it doesn't know about a variable called shape. This is because you declared it in the loop.)
 
Old 10-11-2010, 12:53 AM   #68
graemef
Senior Member
 
Registered: Nov 2005
Location: Hanoi
Distribution: Fedora 13, Ubuntu 10.04
Posts: 2,379

Rep: Reputation: 148Reputation: 148
Just to clarify what I was saying. From your code in post #66
Code:
        if(row<=0)                                                   
        printf("Shape length cannot be negative. Try again\n");      

        char shape=1;
        shape='s','h','f';
The red lines are not needed.
 
Old 10-11-2010, 12:55 AM   #69
sag47
Senior Member
 
Registered: Sep 2009
Location: Raleigh, NC
Distribution: Ubuntu, PopOS, Raspbian
Posts: 1,899
Blog Entries: 36

Rep: Reputation: 477Reputation: 477Reputation: 477Reputation: 477Reputation: 477
While you're at it, since you're scanning keyboard input by passing a reference it is better to declare shape outside of your while loop as you only need declare it once.

Code:
int main()
{
   char shape=1;
   /* declare any other variables which have references within the while loop */
   while(1)
   {
      /* Code to get the value of shape */
      switch (shape)
      {
         case 's':
            /* code to handle the shape 's' */
            break
         /* the remaining case statements */
      } /* end of switch */
   } /* end of while loop forever */

} /* end of main function */
 
Old 10-11-2010, 12:56 AM   #70
boilers969
Member
 
Registered: Oct 2010
Posts: 55

Original Poster
Rep: Reputation: 0
So is this command correct?

Code:
shape='s','h','f';
 
Old 10-11-2010, 12:57 AM   #71
boilers969
Member
 
Registered: Oct 2010
Posts: 55

Original Poster
Rep: Reputation: 0
Wait...I don't even need that line?
 
Old 10-11-2010, 01:06 AM   #72
boilers969
Member
 
Registered: Oct 2010
Posts: 55

Original Poster
Rep: Reputation: 0
Do I need to declare the char s,h, and f anywhere? Because I'm getting the error that they are undeclared. And I understand why.
 
Old 10-11-2010, 01:12 AM   #73
graemef
Senior Member
 
Registered: Nov 2005
Location: Hanoi
Distribution: Fedora 13, Ubuntu 10.04
Posts: 2,379

Rep: Reputation: 148Reputation: 148
When you refer to s you are referring to the character value s. Not a variable that could be any value. but the specific value s. These are called character literals, and in C you enclose them in single quotes. Hence you would have case 's': not case s: Okay.

Last edited by graemef; 10-11-2010 at 01:14 AM.
 
Old 10-11-2010, 01:15 AM   #74
boilers969
Member
 
Registered: Oct 2010
Posts: 55

Original Poster
Rep: Reputation: 0
Ok got it! I got it to compile. But its not working the way it should. It terminates after the first round. Do you want me to post what I got?
 
Old 10-11-2010, 01:19 AM   #75
graemef
Senior Member
 
Registered: Nov 2005
Location: Hanoi
Distribution: Fedora 13, Ubuntu 10.04
Posts: 2,379

Rep: Reputation: 148Reputation: 148
Post your code along with any compiler messages.
 
  


Reply



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] What kind of code has shapes in it? theKbStockpiler Programming 2 09-11-2010 01:33 AM
LXer: IntelliJ's Maia shapes up against Eclipse LXer Syndicated Linux News 0 05-29-2009 11:01 PM
Dual monitors, separate X displays; want firefox in both displays bforbes Linux - Desktop 7 10-15-2008 09:26 PM
redirect a program to 2 displays schneidz Programming 0 08-29-2006 11:02 AM
Need benchmark program which displays bus speed. Arodef Linux - Software 2 07-15-2004 05:12 PM

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

All times are GMT -5. The time now is 02:01 PM.

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