Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

C Program help

Posted on 2013-01-30
6
Medium Priority
?
279 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
6 Comments
 
LVL 84

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 84

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 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.

 

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

Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
Examines three attack vectors, specifically, the different types of malware used in malicious attacks, web application attacks, and finally, network based attacks.  Concludes by examining the means of securing and protecting critical systems and inf…
The goal of this video is to provide viewers with basic examples to understand and use pointers in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use nested-loops in the C programming language.

721 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