[Quick]Formatting output. Adding empty spaces.

Pages: 12
I stole my thread to ask something new.

I read somewhere else how to format the output text, say, add 20 spaces before the actual output comes.

Something like;

cout << [bleh:20] "Hello, world!";

Will come out with 20 spaces before Hello, world!


There was also a way to change the spacer so that you could say use - instead.

But i have lost this page.
Can anyone help me?

I dont need help with my old post, thanks.










Hello

I am trying to write a Loop, currently a while loop since i couldnt get a for loop to work.

I have previously told the used to enter a number in person.number, i now want the loop to call me a cin to place a name into and array, and do that the set number of times.

1
2
3
4
5
6
int n = 0;
while(n<=person.number){
	cout << "Enter the name: ";
	cin >> person.name[1++];
	n++;
}



1. User enter number of persons.
2. The loop loops that set number of times.
3. The loop asks the user for a name each time.
4. That name is then put into an array.
5. The position in the array then moves up one position.

The problem here is with the array.
I keep getting:

\main.cpp(34) : error C2105: '++' needs l-value


How do i tell it to increase the position in the array by one each time the loop runs, for a set number of times(by the user)?


Thank you.
Last edited on
You need to use a variable, not a literal.
Why don't you use 'n' ?
You cannot increment a literal constant value. 1 is a literal number, not a variable value. Also, you have a fencepost error. You probably want:
1
2
3
4
5
6
int n = 0;
while(n<person.number){
	cout << "Enter the name: ";
	cin >> person.name[n];
	n++;
}

A for loop would be similar:
1
2
3
4
for(int n=0; n<person.number; n++){
	cout << "Enter the name: ";
	cin >> person.name[n];
}

Hope this helps.

[edit] too slow...

Also, you probably don't want to use >> to get the names of people. Use getline():
1
2
3
4
for(int n=0; n<person.number; n++){
	cout << "Enter the name: ";
	getline( cin, person.name[n] );
}

Good luck!
Last edited on
@Duoas... in your first example, could that not be re-written to:

1
2
3
4
5
int n = 0;
while(n<person.number){
	cout << "Enter the name: ";
	cin >> person.name[n++];
}


It could be...but you might as well just use a for loop. It looks cleaner anyway.
mcleano: The for loop is cleaner and limits the scope of n. Why would you unpack a perfectly good for loop into a while loop?
Oh no i'm not saying you should, it was for my own knowledge that i was correct in thinking that:

1
2
cin >> person.name[n];
n++;


could be shortened to:

 
cin >> person.name[n++];


I was making no comment on whether a while loop or a for loop should be used, although i too think that a for loop should be used.
Ah, didnt see the responses, usualy get a message to my email.

Anyway, i tried what you said, and it worked, with one small error.

I posts two 'Enter the name: ' strings in the first loop.
So i need to somehow tell it not to do it while n=0.


How many Names do you need?5
Enter the name: Enter the name: Karl
Enter the name: Peter
Enter the name:


*edit*

Oh yeah, and thank you. :)
Last edited on
then do exactly that, implement a while loop with the conditions of,
1
2
3
while(n!=0)
// your code
}
A loop within a loop?
Will try that tomorrow.
Thanks
actually
1
2
if(n!=0)
//your code 
Nopie.
Doesnt work.

I'll post the whole thing tomorrow.

The thing is that it wont run the loop at all.

Ok, here is the full text:


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
#include <iostream>
#include <string>
#include <sstream>
using namespace std;

// The list itself
struct theList {
	string name[10];
	int number;
} person;

int main()


{
//Asks the user for how many names he wants in the list.
cout << "How many Names do you need?";
cin >> person.number;

//Here it is supposed to ask the user for a name to put into the list, and do that the set number of times.
for(int n=0; n<person.number; n++){
	cout << "Enter the name: ";
	getline( cin, person.name[n] );

//Something to hold the console up at the end.
cin >> person.number;
}

return 0; 
}


So that version works, but posts two strings in the first run.
If i nest that into an if or a while loop it doesnt run the loop.

Remove line 26 and use some of these solutions: http://www.cplusplus.com/forum/articles/7312/
after the loop closing brace

1
2
cout << "How many Names do you need?";
cin >> person.number;
You have a fixed number of names in theList
Yes, i know that i have the array at 10, but i only want it to use the amount that the user specifies, this program is just something i do to learn one part in a larger program. And having more slots then the user specifies will be pretty bad, in my case, the game will crash.


So if i tell it i want 3 names, it only asks me for the name 3 times, and only fills the 3 first places in the array. And it wont touch the rest since they wont be used.




And the last cin there is supposed to be outside that brace. Removed some placeholders and commented out older versions of the program so it must have moved.

*edit*

Nevermind, my first version worked if i added the first thing you said;

1
2
3
4
5
6
int n = 0;
while(n<person.number){
	cout << "Enter the name of the building: ";
	cin >> person.name[n];
	n++;
}


*edit 2*

I changed the cin >> into getline as i was told would be better.
And it seems that it was the getline that causes the problem in the first place.
Last edited on
Sorry, forgot to hit 'post' earlier. I may have missed some point:

Pether
It is line 18 that is causing you the problem. Because you are mixing getline() and >>, you will have problems. To fix them, you will have to get rid of that extra newline:
1
2
cin >> person.number;
cin.ignore( numeric_limits <streamsize> ::max(), '\n' );

This is the same technique Bazzy refers you to to hold the console at the end.

Also, as to what Bazzy indicated about the fixed length of your array of names -- what if, when running the program, your professor enters "11" or "94" or some number greater than nine when asked "How many Names do you need?"


I suspect that your professor is looking for a Linked List? Your list should be composed of 'nodes' or 'elements':
1
2
3
4
5
struct node_t
  {
  string  name;
  node_t* next;
  };
And your list could encapsulate the nodes:
1
2
3
4
5
struct list_t
  {
  unsigned count;
  node_t*  first;
  };

You can even add methods to help manage the list:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
struct node_t
  {
  string  name;
  node_t* next;

  node_t( const string& name = string() ):
    name( name ),
    next( NULL )
    { }
  };

struct list_t
  {
  unsigned count;
  node_t*  first;

  list_t():
    count( 0 ),
    first( NULL )
    { }
  };


Mcleano
Yes, you could write it that way, but it is not generally a good idea because people don't expect the control variable to be modified in a non-obvious way. The general rule of thumb is: unless there is compelling reason to do it that way, don't.

Hope this helps.
There is no professor involved.
This is my program, and my program alone.(With help from others ofcourse.)
It is something i am trying to create myself.

I am not doing a c++ course.
I have been learning c++ on my own, in total it counts to one hour or maybe two.


So i have no idea what a linked list is.
Do you have any guides at hand that you could link to?
Need to see what it does, if it is compatible with my final idea for the program.

Also, people who will use this program will know that you cant set a number higher then 7, since that number is hardcoded into the game, and anything else will crash the game.


The name stuff i am posting here is just a tryout for one part of the program.
Hope you understand.
I updated the main post with a new question.
Thank you.
1
2
3
4
5
6
7
8
9
10
11
12
#include <iomanip>
#include <iostream>
using namespace std;

int main()
  {
  cout << "         1         2\n";
  cout << "12345678901234567890\n";
  cout << setw( 20 ) << left  << "Hello world!" << "\n";
  cout << setw( 20 ) << right << "Hello world!" << "\n";  // the default
  return 0;
  }

Hope this helps.
Yea it does.
Thank you yet again.
Pages: 12