-2

I'm new at programming and wish to learn about for loops could someone help me with my code I am suppose to display an asterisk equal to the number between 2 and 20 depending on what the user entered.
Here is my code and I don't know how to proceed with this please help:

#include "stdio.h"
#include "iostream"

main ()
{
  int num1, x;

  printf("Enter a number between 2 and 20: ");
  scan ("%d", &num1);
  getchar();
  while (num1 > 1)
  {
    for (i=1; i<=num1;i++)
    {
      printf ("*");
    }
Koopakiller
  • 2,838
  • 3
  • 32
  • 47
  • Just get rid of your `while (num > 1)` statement. – fuzzything44 Mar 08 '16 at 16:13
  • 8
    Where are you learning from? Please don't take it personally, but pretty much every line has an error or a bad practice. We do have a [recommended book list](http://stackoverflow.com/q/388242/1171191). – BoBTFish Mar 08 '16 at 16:14

3 Answers3

0
#include <iostream>
using namespace std;
int main()
{
  int num;



   cout << "Enter a number between 2 and 20: ";
   cin >> num;

  if(num > 1 && num < 21)
  {
       for(int i = 0; i < num; i++)
       {
           cout << "*" << i << endl;
       } 
  } 

  return 0;
}

I'm not sure I understand your question, so let me ask you this. Suppose the user inputs the number 12, are you then supposed to output 12 Asterisks?

Or are you supposed to display an asterisk before each number, and make the amount of asterisks correspond to said number?

Or are you just printing an asterisk before each number like I've done here?

Moving on, A basic for loop declares a counter variable, takes that counter and checks it against a condition. You'll see in my example that i = 0 and that the for loop executes as long as i < num. the i++ increments the counter.

suroh
  • 917
  • 1
  • 10
  • 26
  • When commenting on answers you should understand how to write basic c++. This code is formated properly and was designed to produce a specific output, You don't have to like it but it's not wrong. @ BoBTFish using namespaces in your code is not bad practice as you can include multiple namespaces it's up to the programmer to realize when some thing will conflict with another namespace, furthermore in simple code like this your comment only serves to show that you're having a bad day and want to try and cause trouble on the internet. – suroh Mar 08 '16 at 16:38
  • @BobTFish I did not open this question, I provided a potential answer to it by showing the Op and describing the basic use of a for loop this individual is new and there is no reason he/she should be thinking about not using a namespace. Your point is moot and your comment misguided. – suroh Mar 08 '16 at 16:42
0

let's just keep it short and simple . we have 2 things to do here -

  • let the user enter a number (suppose,n)
  • print "*" (without inverted commas) n times.

    #include<iostream.h> 
    /*
    
    if compiler is not turboC or is gc++ or any other use #include<iostream> instead 
     also add using namespace std; (just before main and not inside any variable )
    
     */
    
     int main()
     {
    
     int n,i;  //n is what user will enter & i is for-loop variable 
    
       for(i=1;i<=n;i++)
       {
       cout<<"*";  
    
       /*this line will print the asterick the same no. of times the loop  will run
       which is from  1 to n , that is n times */
    
       }
    
     return 0;
    
     }
    

hope that it clears your doubts ! keep questioning ! be curious! :D

0

I take it that you're accepting the integer input from the user and printing that many asterisks.

Here's a very basic solution:

int main()
{
    int numAsterisk, i;    //one stores the number of asterisks, other one used in the loop
    scanf(" %d", &numAsterisk);
    for(i = 1; i <= numAsterisk; i++)
    {

        printf("* ");
    }

    return 0;
}

You may use a 'while' loop instead of the 'for' if you wish, but just remember to update the counter variable, to avoid unfortunate infinite loops.

Happy coding!

Rutvik D
  • 1
  • 1