Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

C Program help

Posted on 2013-01-30
6
Medium Priority
?
280 Views
Last Modified: 2013-03-18
Just started playing with C programming and trying to test out this code.  I have a few questions.

#include<stdio.h>
#include<conio.h>
int main()
{
 int num,r,c,sp;
 printf("Enter loop repeat number(rows): ");
 scanf("%d",&num);
 for(r=1; num>=r; r++)
 {
  for(sp=num-r; sp>=1; sp--)
     printf(" ");
  for(c=1; c<=r; c++)
     printf("%d",c);
  printf("\n");
 }
 getch();
 return 0;
} 

Open in new window


Enter loop repeat number(rows)::  5
        1
      12
    123
  1234
12345

How can I restrict so that someone can't enter more than 10.  Also when they execute the program, I would like for it to repeat all over asking them to enter number of row.  Currently, after the inital enter of number and execute, if I hit "Enter" it exits out.  I want it to ask again "Enter Loop repeat number(Rows):" instead of exiting the program.
0
Comment
Question by:holemania
  • 3
  • 2
6 Comments
 
LVL 85

Expert Comment

by:ozo
ID: 38837554
while( 1 ){
 num=11;
 while( num>10 ){
   printf("Enter loop repeat number(rows): ");
   scanf("%d",&num);
 }
 for(r=1; num>=r; r++)
 {
  for(sp=num-r; sp>=1; sp--)
     printf(" ");
  for(c=1; c<=r; c++)
     printf("%d",c);
  printf("\n");
 }
 getch();
}
0
 

Author Comment

by:holemania
ID: 38837616
Can it be set to prompt that value can't be more than 10?  With what you provided, it will not allow anything more than 10, but can I also prompt that it can't be more than 10 only if when they enter more than 10?
0
 
LVL 85

Accepted Solution

by:
ozo earned 2000 total points
ID: 38837640
while( 1 ){
   printf("Enter loop repeat number(rows): ");
   scanf("%d",&num);
   if( num <=10 ){ break; }
   printf("value can't be more than 10\n");
 }
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 

Author Comment

by:holemania
ID: 38837665
Awesome.  Thanks.
0
 
LVL 9

Expert Comment

by:Subrat (C++ windows/Linux)
ID: 38897485
Just a small change to ozo's reply,

1. This print statement which displays 'Value can't be more than 10" will appear always even if user is providing valid data like value <= 10.
2. So better way would be if user is providing invalid value then we should display this message and repeat the loop.


while( 1 ){
   printf("Enter loop repeat number(rows): ");
   scanf("%d",&num);
   if( num > 10 ){
     printf("value can't be more than 10.\n Please enter a valid value\n");
   }  else {
              break;
   }
 }
0
 

Author Closing Comment

by:holemania
ID: 38996988
Thank you.  Sorry I haven't been on lately and forgot to give you credit.  Thanks again.
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
The goal of this video is to provide viewers with basic examples to understand how to use strings and some functions related to them in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use while-loops in the C programming language.
Suggested Courses

885 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question