LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
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, 01:22 AM   #76
boilers969
Member
 
Registered: Oct 2010
Posts: 55

Original Poster
Rep: Reputation: 0

Code:
#include<stdio.h>

int main()
{
char shape=1;
int row;
 while(1)
        {


        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");      
        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;
}
Compiler messages:

shape.c: In function âmainâ:
shape.c:27: warning: ârowâ may be used uninitialized in this function
shape.c:35: warning: ârowâ may be used uninitialized in this function
shape.c:52: warning: ârowâ may be used uninitialized in this function
 
Old 10-11-2010, 01:25 AM   #77
graemef
Senior Member
 
Registered: Nov 2005
Location: Hanoi
Distribution: Fedora 13, Ubuntu 10.04
Posts: 2,379

Rep: Reputation: 148Reputation: 148
you have a break right at the end that will break out of the loop, remove that.
 
Old 10-11-2010, 01:30 AM   #78
boilers969
Member
 
Registered: Oct 2010
Posts: 55

Original Poster
Rep: Reputation: 0
Here is a sample run:

Enter shape type (s/h/f):h
Enter shape length: 6

Enter shape type (s/h/f):Enter shape length:

Obviously its not showing the shape. And I need "Enter shape type (s/h/f):Enter shape length:" to be seperate lines.
 
Old 10-11-2010, 02:09 AM   #79
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
Quote:
Originally Posted by boilers969 View Post
Code:
#include<stdio.h>

int main()
{
char shape=1;
int row;
 while(1)
        {


        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");      
        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;
}
Compiler messages:

shape.c: In function âmainâ:
shape.c:27: warning: ârowâ may be used uninitialized in this function
shape.c:35: warning: ârowâ may be used uninitialized in this function
shape.c:52: warning: ârowâ may be used uninitialized in this function
You seem to completely ignore what you've been told/asked.

This:

Code:
           int i,row;
           for(i=row;i>=1;i--)
piece of code is obviously wrong, you have already been told so, you have already been told about your lack of understanding of the very basic programming concept of variables initialization, yet you continue to write the same erroneous code.

Likewise,

Code:
                int i,j,k,row,m;
                m=row;
is obviously wrong.

Do you understand English ? What does C99 standard say on variables initialization ? What makes you think the variables are initialized ? Why do you even try to run the code if the compiler issues warnings ?
 
Old 10-11-2010, 02:19 AM   #80
graemef
Senior Member
 
Registered: Nov 2005
Location: Hanoi
Distribution: Fedora 13, Ubuntu 10.04
Posts: 2,379

Rep: Reputation: 148Reputation: 148
You have declared row right at the start of the main function you don't need to do it again (you have done so inside the case blocks). By declaring a variable with the same identifier in a block of code you hide the original local variable. In this case row had a value in it that the user had entered, but by redeclaring it you are hiding that value.
 
Old 10-11-2010, 03:24 PM   #81
boilers969
Member
 
Registered: Oct 2010
Posts: 55

Original Poster
Rep: Reputation: 0
Its working just fine. The only thing I'm having problems with is terminating the program when the user enters a char other than s,h,f.

Code:
 if(shape!=s/h/f)
         return 0;
This is not working. It's giving me the following error. Which says they are undeclared.
Code:
shape.c: In function âmainâ:
shape.c:22: error: âsâ undeclared (first use in this function)
shape.c:22: error: (Each undeclared identifier is reported only once
shape.c:22: error: for each function it appears in.)
shape.c:22: error: âhâ undeclared (first use in this function)
shape.c:22: error: âfâ undeclared (first use in this function)
 
Old 10-11-2010, 03:46 PM   #82
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
That has already been answered here.

Before you take the time to ask any more questions please go through and read the responses we have already given you more carefully. Be sure to fully understand them.

And stop saying you understand when you don't understand.

You should look up on how to interpret compiler messages as well...

Quote:
shape.c:22: error: âsâ undeclared (first use in this function)
Means that there is a problem in the file shape.c on line 22. Look at line 22 of your code and figure out why it won't work.

Last edited by sag47; 10-11-2010 at 03:48 PM.
 
Old 10-11-2010, 04:00 PM   #83
boilers969
Member
 
Registered: Oct 2010
Posts: 55

Original Poster
Rep: Reputation: 0
I know how to read compiler messages. But in this earlier post you told me I had it right, and it doesn't work. I've tried several different things.

You had it before:
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");
      if(shape!=s/h/f)
         return 0;
   }
}
 
Old 10-11-2010, 04:09 PM   #84
Nylex
LQ Addict
 
Registered: Jul 2003
Location: London, UK
Distribution: Slackware
Posts: 7,464

Rep: Reputation: Disabled
Quote:
Originally Posted by boilers969 View Post
if(shape!=s/h/f)
What exactly are you trying to test here? Do you really want to divide s by h and then by f and compare that with shape?

Last edited by Nylex; 10-11-2010 at 04:11 PM.
 
Old 10-11-2010, 04:12 PM   #85
boilers969
Member
 
Registered: Oct 2010
Posts: 55

Original Poster
Rep: Reputation: 0
My goal is to get the program to terminate if the user enters something other than "s,f,h". If they keep entering those then the program will keep running.
 
Old 10-11-2010, 04:16 PM   #86
Nylex
LQ Addict
 
Registered: Jul 2003
Location: London, UK
Distribution: Slackware
Posts: 7,464

Rep: Reputation: Disabled
How do you think your if statement is testing that? You've specified that the value of the variable shape be compared with the value of s/h/f, where s, h and f are all variables. You can do division with char variables, because they're treated as integers, I believe. However, I don't think that's what you intend to do..

Last edited by Nylex; 10-11-2010 at 04:20 PM.
 
Old 10-11-2010, 04:21 PM   #87
boilers969
Member
 
Registered: Oct 2010
Posts: 55

Original Poster
Rep: Reputation: 0
I don't quite get your explanation. To me it seems like it should work because scanf stores either s,h,f into shape. And then if shape does not equal one of those it will terminate. That's how I read it.
 
Old 10-11-2010, 04:26 PM   #88
Nylex
LQ Addict
 
Registered: Jul 2003
Location: London, UK
Distribution: Slackware
Posts: 7,464

Rep: Reputation: Disabled
It seems that you're confused. You've written s/h/f and you're testing that this is not equal to the value stored in shape. Do you agree that / is for division? Do you also agree that you're referring to variables s, h and f? Since you haven't put those characters between single quotes, they refer to variables and not character literals 's', 'h' and 'f'. You haven't declared variables s, h and f, hence the compiler errors. Also, you probably want to look up the or operator in C..

Last edited by Nylex; 10-11-2010 at 04:32 PM.
 
Old 10-11-2010, 04:32 PM   #89
boilers969
Member
 
Registered: Oct 2010
Posts: 55

Original Poster
Rep: Reputation: 0
They were telling me to use / earlier but yes that means division. The or operator is '||'.

This looks reasonable to me:
Code:
 if(shape!='s'||'h'||'f')
                return 0;
 
Old 10-11-2010, 04:36 PM   #90
Nylex
LQ Addict
 
Registered: Jul 2003
Location: London, UK
Distribution: Slackware
Posts: 7,464

Rep: Reputation: Disabled
Quote:
Originally Posted by boilers969 View Post
Code:
 if(shape!='s'||'h'||'f')
                return 0;
Looks fine to me.
 
0 members found this post helpful.
  


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 07:04 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