Solved

C Program help

Posted on 2013-01-30
6
277 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 500 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
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…
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.
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

752 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