Solved

C Program help

Posted on 2013-01-30
6
270 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
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
ScreenConnect 6.0 Free Trial

Discover new time-saving features in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI, app configurations and chat acknowledgement to improve customer engagement!

 

Author Comment

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

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

Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

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…
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…
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.
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.

911 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

26 Experts available now in Live!

Get 1:1 Help Now