spaces problem

im tring to practice on space looping i got the loop to work but how do you make it so i dont have to enter 1 first?





#include <iostream>
using namespace std;

{

int spaces1,spaces;


cout<<"please enter 1 then the ammount of spaces you wish"<<endl;
cin >>spaces1;

cin >>spaces;

while(spaces1 <= spaces)
{
(spaces1 <= spaces);
cout<<""<<endl;
spaces1++;
}

}
You don't need the extra (spaces1 <= spaces) so that can go. Two options work fine:

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
31
32
33
34
35
36
37
38
39
#include <iostream>
using namespace std;

int main()
{
	int spaces1 = 1; // initialize spaces1 to a value of 1
	int spaces; 
	cout << "please enter the amount of spaces you wish" << endl;
	cin >> spaces;
	
	while( spaces1 <= spaces )
	{
		cout << "*" << endl;
		spaces1++;
	}
		
	system("pause");
	return 0;
} 

// OR use a for statement like this:

#include <iostream>
using namespace std;

int main()
{
	int spaces1;
	int spaces; 
	cout << "please enter the amount of spaces you wish" << endl;
	cin >> spaces;
		
	for ( spaces1 = 1; spaces1 <= spaces; spaces1++)
	               cout << " " << endl;
		
	
	system("pause");
	return 0;
} 


return 0;
The std::string constructor will allow you to create a string of any character you wish (see line 22):
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
#include <iostream>
#include <sstream>
#include <string>
using namespace std;

int main()
  {
  char   ch;
  int    n;
  string s;

  cout << "Please enter a character (defaults to a space): ";
  getline( cin, s );
  if (s.empty()) ch = ' ';
  else           ch = s[ 0 ];

  cout << "Please enter the number of times to repeat that character (minimum = 1): ";
  getline( cin, s );
  if (!(stringstream( s ) >> n)) n = 1;
  if (n < 1) n = 1;

  cout << "begin>" << string( n, ch ) << "<end\n";

  return 0;
  }

Hope this helps.
Last edited on
get line i know i should of used that thanks
Topic archived. No new replies allowed.