friendzzz help

this my code for reversing and change small letters into capital vice versa....but problem is that when enter ehsan is good then it covert only is good and reverse only is good......without space it work...with space it does not work

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
 #include <iostream>
#include <conio.h>
#include <string>
#include <stdio.h>
#include <string.h>
 using namespace std;
int main()
{
   char input[1000];

   cout <<"Enter input  == ";
   cin >>input;
   cin.getline( input ,  1000   );
   int i;
  for(i=0;i<=strlen(input);i++)
   {
	 if(input[i]>=65 && input[i]<=90)
	{
		   input[i]=input[i]+32;
	}else
	if(input[i]>=97 && input[i]<=122)
           input[i]=input[i]-32;
   }
   {
   cout<<endl<<"small lettes into capital and vice versa == "<<input;
   }
   strrev(input);
   cout<<endl<<"Reverse input == " << input;
   getch();
   return 0;
}
reply fast
first, do not mix string with string.h, they will cause conflicts.

The cause of your problem is that cin stops reading when it encounters spaces or newlines, thus causing unexpected results

If you want to read with spaces, use getline() instead:
cin.getline ( input, 1000 );
or another version:
getline ( cin, input ) // you need to include string
but the first is recommended

EDIT
the 2 differ by in which you will store the processed input

the first example is recommended for c - string with limited length such as your input array which has a limit of 1000, the 1000 parameter in getline is used to limit the characters which maybe entered and stored into input, which otherwise if the characters entered is greater than the array it will cause unexpected results.

the 2nd is recommended for std::string types since string automatically manages memory, thus you will not need a limit...
Last edited on
but the first is recomnended
In C++ it is recommended to use std::strings instead of c-strings.
thanksssss.....problem solve
Topic archived. No new replies allowed.