HELP with space bug

can anyone help me to see what is the problem here??
when ever I type and space... there is a loop with the first word repeating it self

and even if I do like "mario 25" it actually prints mario 25 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
25
26
27
28
#include <iostream>
#include <stdio.h>
#include <string>

using namespace std;

int main()
{
    string MyName;
    cout<<"Hello! May I Have Your First Name?"<<endl;
    cout<<"\n";
    cin>>MyName;
    cout<<"How many times would you want me to display your first name?";
    cout<<"\n";
    int NamePrnt;
    int counter=1;
    cin>>NamePrnt;

    while(counter<=NamePrnt)
    {
        cout<< MyName;
        cout<< "\n";
        counter++;
    }


    return 0;
}
Last edited on
First of all use the <> tags. Define the variables at the beginning of the code.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
 #include <iostream>
#include <stdio.h>
#include <string>

using namespace std;

string MyName;
int NamePrnt;
int counter=1;

int main()
{

    cout<<"Hello! May I Have Your First Name?"<<endl;
    cout<<"\n";
    cin>>MyName;
    cout<<"How many times would you want me to display your first name?";
    cout<<"\n";

    cin>>NamePrnt;
    while(counter<=NamePrnt)
    {
        cout<< MyName;
        cout<< "\n";
        counter++;
    }


    return 0;
}

if I use that... now i cannot do the second input..
The loop will iterate as many times as the positive value in NamePrnt

while(counter<=NamePrnt)
{
cout<< MyName;
cout<< "\n";
counter++;
}

I don't unsdestand your post vlad...
can you explain it please?
closed account (zb0S216C)
I recommend switching your while loop in favour of the for loop:

1
2
3
std::cin >> NamePrnt;
for(int Count(0); Count < NamePrnt; ++Count)
    std::cout << MyName << '\n';

If you don't know the basic structure of the for loop, here's a basic overview:

The for loop has 4 sections:

1
2
for(Section A; Section B; Section C)
    Section D;

Section A) Initialisation. Here, you'll either declare a variable, or assign an existing variable a new value.
Section B) Condition. While this condition is not met (is false), your loop will continue.
Section C) Increment/Decrement. After every cycle (or iteration) of your loop, section C is executed.
Section D) Body. A no-brainer, really.

For more information, go here: http://www.cplusplus.com/doc/tutorial/control/#for

Wazzak
It did not fix it Framework...
I keep getting the same bug..
What bug are you speaking about? You wrote the program such a way that if you enter
mario 25


it prints mario 25 times.

What do you want?
Last edited on
what I want
is that i can write mario robles
then i can input 25
because if i write mario robles... there is an infinite loop of printing mario
operator << for strings enters only one word separated by white spaces. You should use getline function.
can you help me out with that??
because I have totally no idea how to use it..
No worries I fix it allready
vlad thanks a lot your last comment was the actual solution :D
Last edited on
Topic archived. No new replies allowed.