looping basics

Input a name and print it ten times
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
  #include<iostream>
	
    using namespace std;

	int main()
		
     {
        int a;
        
        do
        {
            cout<<"Enter your name: \n";
            cin>>a;
            
            a++;
            
        }
        
        while(a<10);
        
         return 0;   
                     
   	}


please help and dont overdo the codes use the codes only written their thanks
its just basic i know the logic but it exits
closed account (Dy7SLyTq)
you need to take another look at how variables work. i recommend using this site. and i dont know if its just your text editor/ide, but your formatting is horrible
Yeah. You need to learn both how variables work(you used incorrect type), how loops work(you aren't even printing this variable). And as DTS said, try to improve your formatting.

Cheers!
First of all take a look at your variables and what you are trying to do with them.
You are trying(I guess) to input a string into a variable where it only accepts an integer.

Tip: Its always good to initialize variables when you declare them even thought they might not have a valu yet, they can be null.
The people here have given some useful tips. As stated above, please take a look at variables. The int variable is for storing numbers. If you want to store, for example a name, you need to declare the string variable.

And also, I suggest using a for loop. I made a simple code so you can understand the basics:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <iostream>

int main()
{
    std::string name;
    const int times = 10;
    std::cout << "Please input your name: ";
    std::cin >> name;
    
    for (int i = 0; i < times; i++) {
        std::cout << name << std::endl;
    }
    
    return 0;
}
try this

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>

#include <string>
//puts std:: into the namespace so you dont have to. 
using namespace std; 

int main()
{
    string name; 
     int i;

        cout<<"Please enter your name: "; getline(cin,name); 

       for(i=0; i<10; ++i)
         {
        cout<<name<<"\n"; 
         }


//getline will return your entire string, like if you have Firstname Lastname,



}
Topic archived. No new replies allowed.