LinuxQuestions.org
Share your knowledge at the LQ Wiki.
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-10-2010, 09:20 PM   #46
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
My goal is to have the program keep running and never terminate unless a user enters a char other than s,h,f.
Okay with that in mind don't worry about drawing the shapes yet but concentrate on the s,h,f terminate functionality. Starting with this segment of code that you already have:

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");
   }
}
Can you modify this code to achieve your first goal?
 
Old 10-10-2010, 09:37 PM   #47
boilers969
Member
 
Registered: Oct 2010
Posts: 55

Original Poster
Rep: Reputation: 0
Would something like this work?

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)
         then terminate the program(i dont know the command to terminate it)
   }
}
 
Old 10-10-2010, 10:07 PM   #48
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
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-10-2010, 10:09 PM   #49
graemef
Senior Member
 
Registered: Nov 2005
Location: Hanoi
Distribution: Fedora 13, Ubuntu 10.04
Posts: 2,379

Rep: Reputation: 148Reputation: 148
First I would restructure it a little. Check that a valid option has been entered s, h or f before asking for the length.

Second the if conditional needs to be broken down into three parts: is it a 's' or is it a 'h' or is it a 'f'. Often it is easier to consider the positive outcome (was a valid value entered) rather that the negative outcome (was an invalid value entered). The negative outcome will be part of the else statement.

To exit from the program you can use the return statement.
 
Old 10-10-2010, 10:10 PM   #50
boilers969
Member
 
Registered: Oct 2010
Posts: 55

Original Poster
Rep: Reputation: 0
So looking at the next goal. I want to output something for what they input. So is it alright to use 3 different switch statements?

Last edited by boilers969; 10-10-2010 at 10:24 PM.
 
Old 10-10-2010, 11:25 PM   #51
boilers969
Member
 
Registered: Oct 2010
Posts: 55

Original Poster
Rep: Reputation: 0
So looking at the next goal. I want to output something for what they input. So is it alright to use 3 different switch statements?
 
Old 10-10-2010, 11:29 PM   #52
graemef
Senior Member
 
Registered: Nov 2005
Location: Hanoi
Distribution: Fedora 13, Ubuntu 10.04
Posts: 2,379

Rep: Reputation: 148Reputation: 148
Use a single switch statement with three case statements one for each legal input value. The default case will indicate an invalid value.

If you think carefully about it the switch statement actually means that you can throw away the if statement.
 
Old 10-10-2010, 11:30 PM   #53
boilers969
Member
 
Registered: Oct 2010
Posts: 55

Original Poster
Rep: Reputation: 0
Ok this is where I get confused. I had it as a single switch statement and then changed it. I thought you could only do a switch on one variable?
 
Old 10-10-2010, 11:38 PM   #54
graemef
Senior Member
 
Registered: Nov 2005
Location: Hanoi
Distribution: Fedora 13, Ubuntu 10.04
Posts: 2,379

Rep: Reputation: 148Reputation: 148
you can only do a switch on one variable but you will have different case statements for each value that variable can take.

For example:
Code:
switch( i ) 
{
    case -1:
        n++;
        break;
    case 0 :
        z++;
        break;
    case 1 :
        p++;
        break;
}
gratuitously copied from here.
 
Old 10-10-2010, 11:41 PM   #55
boilers969
Member
 
Registered: Oct 2010
Posts: 55

Original Poster
Rep: Reputation: 0
So in my case would this work? Except of course I will have to add more.

Code:
switch(shape)
{
case s;
   break;
case h;
   break;
case f;
   break;
}
 
Old 10-11-2010, 12:08 AM   #56
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
I believe you have to signify that a char has been selected. Strings are wrapped in double quotes " and char (or single characters) are wrapped in single quotes '. So you would need something along the lines of.

Lets say the user presses c.
Code:
char shape=1;
scanf("%c",&shape);
Then shape='c'.

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

Original Poster
Rep: Reputation: 0
So I'm confused as to what I switch.
Would this work?

Code:
switch(char 's','h','f')
{
case s;
   break;
case h;
   break;
case f;
   break;
}
 
Old 10-11-2010, 12:17 AM   #58
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
Here, now that you're getting to know C some more check out paulsm's example again.

His switch statement is formatted how I described.
 
Old 10-11-2010, 12:20 AM   #59
boilers969
Member
 
Registered: Oct 2010
Posts: 55

Original Poster
Rep: Reputation: 0
Do I need to put the switch into the while loop? I think I'll try that.
 
Old 10-11-2010, 12:24 AM   #60
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
The case is the same thing as the conditional if statement but more compact.

Code:
char shape=1;
shape='c';
if(shape=='a')
{
    return 0;
}
else if(shape=='b')
{
    return 0;
}
else
{
    return 1;
}
There the if statements are comparing the value stored in the shape variable to different conditions. Like if the contents of shape is equal the the character b then do something. Here is the exact same statement in a case...
Code:
char shape=1;
shape='c';
switch(shape)
{
    case 'a':
        return 0;
    case 'b':
        return 0;
    default:
        return 1;
}
It does the same thing as the if statements. Does this explain the how the case works a little?
 
  


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 11:37 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