Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

display a string

Status
Not open for further replies.

Guest_imported

New member
Jan 1, 1970
0
I am studying for a final and I have a practice exercise that wants me to display a string with one letter per line.

I am supposed to do this with a for, and while loop.

I can only get it to display all letters on one line.

cin.getline(stringName, 20);

for ( I am confused )

while ( still confused)

HELP !!!
 
Create second pointer to string.
within your FOR loop display to character pointed to by this
pointer. Then increment the pointer.
 
I am still confused. Was not going to use a pointer, but will try it.

Not sure how to set this up for loop, or while loop

 
try this instead

Code:
#include <iostream.h>

void main()
{
	char szStr[41];
	int nI;

	cout<<&quot;Enter A String : &quot;;
	cin.getline(szStr,40,'\n');

	for(nI=0;szStr[nI]!='\0';nI++)
	{
		cout<<szStr[nI]<<endl;
	}
}

or if u wanna do using pointers try the below code

Code:
#include <iostream.h>

void main()
{
	char szStr[41];
	char *pStr;

	cout<<&quot;Enter A String : &quot;;
	cin.getline(szStr,40,'\n');

	for(pStr = szStr;*pStr!='\0';pStr++)
	{
		cout<<*pStr<<endl;
	}
}

regds

sridharan
 
Thanks sridharan;

Looking at this it looks like this would only display my char strng on one line.

cout<<&quot;Enter A String : &quot;;
cin.getline(szStr,40,'\n');

for(nI=0;szStr[nI]!='\0';nI++)
{
cout<<szStr[nI]<<endl;
}
}

//OUTPUT

//Enter A String :
Will this work
//output
Will this work

// What I want is this;
W
i
l
l

t
h
i
s

w
o
r
k

// I have not ran the code I have only looked at it.
// Also what I want the my output to do is put a space between each character.

//example
cin.getline(strng,40) // Hello World

cout<<strng;
H e l l o W o r l d

your help would be greatly appreciated.
 
cout<<&quot;Enter A String : &quot;;
cin.getline(szStr,40,'\n');

for(nI=0;szStr[nI]!='\0';nI++)
{
cout<<szStr[nI]<<endl;
}
}

This will display the text the way you need it since after each character is displayed endl is called (this'll make your program jump to the beginning of the next line on the screen)
W
i
l
l

t
h
i
s

w
o
r
k

Regarding the second request try this

for (int i=0;i<80;i++)
{
if (i%2==0) cout<<strng[i%2];
else cout << ' ';
}
I'm not entirely sure if this'll work since I haven't tried it but if it doesn't try messing with code ;)
 
What would be the way I could perform the above using a while statement. I know I would need a counter

i++

// but not sure how to perform.


 
jus change the for loop to while loop and increment the variable within the loop....

like this.....

Code:
#include <iostream.h>

void main()
{
    char szStr[41];
    int nI=0;

    cout<<&quot;Enter A String : &quot;;
    cin.getline(szStr,40,'\n');

    while(szStr[nI]!='\0')
    {
        cout<<szStr[nI]<<endl;
	nI++;
    }
}

as told by Eof81 the program will print one character per line coz of endl which will position the cursor to the starting position of the next line something like '\n' in good old printf function.....

regards

sridharan
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top