How to make n character appear 3times using the while loop

Oct 6, 2012 at 3:04pm
ive been trying to create a c++ program that will display my name three times using the while loop but it keeps going wrong after all my try. please if anyone can help show me how is done i will be very grateful thanks
Oct 6, 2012 at 3:08pm
1
2
3
4
5
6
7
int i = 0;

while (i<3)
{
  cout << name;
  i++;
}
Oct 7, 2012 at 2:12am
Thanks very much but don't i need to put the #include<iostream>, using namespace std; system("pause") and return 0;
Oct 7, 2012 at 2:23am
> don't i need to put the #include<iostream>

Yes.


> using namespace std;

You may. Though std::cout << name ; is perhaps better.


> system("pause") ;

No. If you want to pause the program till the user hits enter: std::cin.get() ;


> return 0;

You may, though you need not.
If there is no return statement at the end of main(), return 0 ; is implicitly assumed.

Last edited on Oct 7, 2012 at 2:24am
Oct 7, 2012 at 3:03am
can you write how it ought to be pls cos am not an expert in this just started it. Thanks
Oct 7, 2012 at 3:23am
First write a simple 'Hello World' program.
1
2
3
4
5
6
7
#include <iostream>

int main()
{
    std::cout << "Hello World\n" ;
    std::cin.get() ;
}

Make sure that you understand this before moving on to the next step.
For instance, look up std::istream::get() ;
http://www.cplusplus.com/reference/iostream/istream/get/

Then write a program to accept a name from the user and print it out.
1
2
3
4
5
6
7
8
9
10
11
#include <iostream>
#include <string>

int main()
{
    std::string name ;
    std::cout << "Please enter you name: " ;
    std::getline( std::cin, name ) ;
    std::cout << "your name is: " << name << '\n' ;
    std::cin.get() ;
}

Make sure that you understand this before moving on to the next step.
For instance, look up std::string and std::getline()

Then move on to writing a loop to print the name three times.
Oct 7, 2012 at 3:27am
thanks
Topic archived. No new replies allowed.