Beginner C/C++

Moderators: None (Apply to moderate this forum)
Number of threads: 5430
Number of posts: 16951

This Forum Only
Post New Thread
Single Post View       Linear View       Threaded View      f

Report
Struggling with some basic code Posted by peskypea on 6 Nov 2012 at 1:55 AM
Hi all,

Im a beginner with any form of coding and im trying to solve a problem but the lightbulb has not switched on just yet...

ive managed to scramble the code below to allow me to do part of what I wanted but im struggling with the data input side.

Basically I want to add something to the code below but im not sure where to put it at present. I would like the user to be prompted for a number (get int possible to use?) between 1-50 which will then return the suitable output (the number inputted would denote the number of rows) but I want to include a loop to ensure that if I dont enter a number between 1-50 it just goes back to the start and asks again.

Apologies if im not making much sense, its a simple problem but im just not seeing where I can put the code or how to exactly construct it.

Any help would be greatly appreciated.

Many Thanks


#include<stdio.h>
 
int main()
{
    int n, c, k, space;
 
    printf("Enter number of rows\n");
    scanf("%d",&n);
    if (n >23) { 
    printf ("number too high\n");
    }
 
    space = n;
 
    for ( k = 1 ; k <= n ; k++ )
    {
        for ( c = 1 ; c < space ; c++ )
            printf(" ");
 
        space--; 
 
        for( c = 1 ; c <= k ; c++ )
            printf("#");
 
        printf("\n");
    }
 
    return 0;
}







 

Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic
© Copyright 2011 Programmersheaven.com - All rights reserved.
Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
Operated by CommunityHeaven, a BootstrapLabs company.