Solved

C Program help

Posted on 2013-01-30
6
269 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 84

Expert Comment

by:ozo
Comment Utility
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
Comment Utility
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
Comment Utility
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
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 

Author Comment

by:holemania
Comment Utility
Awesome.  Thanks.
0
 
LVL 8

Expert Comment

by:Subrat (C++ windows/Linux)
Comment Utility
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
Comment Utility
Thank you.  Sorry I haven't been on lately and forgot to give you credit.  Thanks again.
0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Suggested Solutions

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…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
The goal of this video is to provide viewers with basic examples to understand recursion in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use for-loops in the C programming language.

771 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

Need Help in Real-Time?

Connect with top rated Experts

7 Experts available now in Live!

Get 1:1 Help Now