Or-conditioned while loop not working

The purpose of this code is to take a c-string of many random words and then write these words in reverse. However my while statements keep on looping when i added an "or" condition for them. I really can't figure out why ..

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>
#include <cstring>
#include <fstream>
#include <cmath>
#include <cctype>
using namespace std ;

int main()
{
	char s1[100] ;
	int count1=0 ;
	int x, max ;

	cout << "Enter words in one sentence: " << endl ; cin.getline(s1, 100) ;

	cout << "Printing words in reverse: " << endl ;
	while (s1[count1] != '\0')
	{
		count1++ ;
	}
	max= count1 ;
	while (count1 >= 0)
	{
		while ((s1[count1] != ' ') || (count1 == 0))
		{
			count1-- ;
		}
		x= count1 ;
		count1++ ;
		while ((count1 < max) || (s1[count1] != ' '))
		{
			cout << s1[count1] ;
			count1++ ;
		}
		cout << endl ;
		count1= x-1 ;
	}
	return 0 ;
}
closed account (zybCM4Gy)
!= || != isn't gonna work it'll always be true. Try != && != else == || ==
Gotta love the output:
Enter words in one sentence:
Hi there, how are you?
Printing words in reverse:
you?  ”\Ãw.¥Ãwè
Æwüþ" `Ã   /NÃw)NÃwïþ" '$$     @@ Ôþ"ÿ" àÿ" ”\ÃwP(Áwÿÿÿÿ)NÃwBN
Ãw@@ 0ÿ"     '$$ ïþ"  ÿ" @             @    ¨?>-> 
8ÿ" œþ"           ÌƽŠd¬/´   Ø4RQ@    ¨?>          ¬¬/´ `ý       ðÿ"
…&@    ˜ÝˆˆºÜ|lw|þÿÿÿ           øÿ" ow|        `ýˆ¬/Èÿ" ˜Ýˆˆÿÿÿÿ°šƒ|xw|
           p&@     Actx
are you?  ”\Ãw.¥Ãwè
Æwüþ" `Ã   /NÃw)NÃwïþ" '$$     @@ Ôþ"ÿ" àÿ" ”\ÃwP(Áwÿÿÿÿ)N
ÃwBNÃw@@ 0ÿ"     '$$ ïþ"  ÿ" @             @    ¨?>-> 
    8ÿ" œþ"           ÌƽŠd¬/´   Ø4RQ@    ¨?>          ¬¬/´ `ý    
ðÿ" …&@    ˜ÝˆˆºÜ|lw|þÿÿÿ       øÿ" ow|        `ýˆ¬/Èÿ" ˜Ýˆˆÿÿÿÿ°šƒ|xw|
           p&@     Actx
how are you?  ”\Ãw.¥Ãwè
Æwüþ" `Ã   /NÃw)NÃwïþ" '$$     @@ Ôþ"ÿ" àÿ" ”\ÃwP(Áwÿÿ
ÿÿ)NÃwBNÃw@@ 0ÿ"     '$$ ïþ"  ÿ" @             @    ¨?>-> 
        8ÿ" œþ"           ÌƽŠd¬/´   Ø4RQ@    ¨?>          ¬¬/´ `ý
   ðÿ" …&@    ˜ÝˆˆºÜ|lw|þÿÿÿ           øÿ" ow|        `ýˆ¬/Èÿ" ˜Ýˆˆÿÿÿÿ°
šƒ|xw|            p&@     Actx
there, how are you?  ”\Ãw.¥Ãwè
Æwüþ" `Ã   /NÃw)NÃwïþ" '$$     @@ Ôþ"ÿ" àÿ" ”\Ã
wP(Áwÿÿÿÿ)NÃwBNÃw@@ 0ÿ"     '$$ ïþ"  ÿ" @             @    ¨?>-> 
               8ÿ" œþ"           ÌƽŠd¬/´   Ø4RQ@    ¨?>          ¬¬/´
`ý       ðÿ" …&@    ˜ÝˆˆºÜ|lw|þÿÿÿ    øÿ" ow|        `ýˆ¬/Èÿ" ˜Ýˆˆÿÿÿÿ°
šƒ|xw|            p&@     Actx
 ÿ8þ" âëÊo›þ" ÄCÌo8þ" ­Èo
   \þ"       xþ" ¦ïÊo    ›þ" 
ËoÀCÌo‚P@        @@ ×ÿÿÿ<ÿ" ½]@ ÀCÌo›þ"    iÿÿÿ    `Ã   Hi there, how are yo
u?  ”\Ãw.¥Ãwè
Æwüþ" `Ã   /NÃw)NÃwïþ" '$$     @@ Ôþ"ÿ" àÿ" ”\ÃwP(Áwÿÿÿÿ)NÃwBNÃw
@@ 0ÿ"     '$$ ïþ"  ÿ" @             @    ¨?>->                        8ÿ
" œþ"           ÌƽŠd¬/´   Ø4RQ@    ¨?>          ¬¬/´ `ý       ðÿ" …&
@    ˜ÝˆˆºÜ|lw|þÿÿÿ     øÿ" ow|        `ýˆ¬/Èÿ" ˜Ýˆˆÿÿÿÿ°šƒ|xw|
   p&@     Actx

Anyways, your conditions are wrong.

The first should be
while (count1 >= 0 && (s1[count1] != ' '))
(while we haven't run past the beginning of the string and we also haven't backed up into a space)

and the second should be
while ((count1 < max) && (s1[count1] != ' '))
(while we haven't run into the end of the string and we also haven't hit a space).
Topic archived. No new replies allowed.