program problem

The program is ok bar the void changecase it displays the string1 fine in output but string2 does not display properly.Does anyone know how to fix this

#include<iostream>
#include<conio>
//Name:
//Author:
//Date:

void searcher(void);
void reverse(void);
void chngcase(void);

char string1[20], string2[20];

main()
{
int opt;
cout<<"Please enter first string:";
cin>>string1;
cout<<"Please enter second string:";
cin>>string2;
do{
clrscr();
cout<<" Main Menu"<<endl<<endl;
cout<<"1) Search string for particular character"<<endl;
cout<<"2) Reverse the string"<<endl;
cout<<"3) Change the case of the string"<<endl;
cout<<"4) Reverse and change the case of the string"<<endl;
cout<<"5) Exit program"<<endl;

cout<<"***Please enter your choice***";
cin>>opt;

if(opt == 1)
searcher();

if(opt == 2)
reverse();

if(opt ==3)
chngcase();

if(opt ==4)
{
reverse(),chngcase();

}


}while(opt !=5);

}
//------------------------------------…
void searcher()
{
char ch;
int x=0,fnd=0;


cout<<"Please enter char to search for:";

cin>>ch;


while(string2[x] != '\0')
{
if (string2[x] == ch)
{
cout<<"Found character in position "<<x+1<<" in "<<string1<<string2<<endl;
fnd++;
}
x++;
}
if (fnd == 0) //fnd var same as start so no char match in string?
cout<<"Sorry character not found in "<<string2<<endl;

cout<<"Press any key to continue...";
getch();
}

//------------------------------------…

void reverse()
{
int x,l;
cout<<"The string reversed is:"<<endl;

l=strlen(string2)-1;

cout<<string1;

for(x=l;x>=0;x--)
cout<<string2[x];





cout<<" Press any key to continue...";
getch();
}




//------------------------------------…
void chngcase()
{
int w,m,x,l;

l=strlen(string2)-1;
cout<<"the 2nd string with its case changed:"<<string1;

for(x=l;x>=0;x--)
{


{
if(string2[x]>= 'A' && string2[x] <='Z') //change from upper to lower

w = string2[x];
m=w+32;
cout<<char(m);
}

{
if(string2[x]>= 'a' && string2[x] <='z') //change from lower to upper

w = string2[x];
m=w-32;
cout<<char(m);
}

cout<<endl;
cout<<"Press any key to continue...";

getch();
}
}
//end of for loop
//end of the function change_case()

Additional Details
The part i need fixed is the changecase it takes in two strings (string1,string2) and the the changecase function changes the 2nd from lower to upper or upper to lower it only does this for string2 but in the output it displays both strings eg:"the case with its string changed: bobMARLEY
What strings did you input in main? I don´t understand in bobMARLEY wich should be string1 and wich should be string2...

You can use code snippets to communicate your code in a more readable fashion, just use
1
2
 the button
with <> at the right


it looks like when you assign cin buffer to string2 it still has the contents of string1... try to prove that, then work to solve it.

your use of braces {} is at least strange, I´m not shure whether you really wanted to do this:

1
2
3
4
5
6
7
{
if(string2[x]>= 'A' && string2[x] <='Z') //change from upper to lower

w = string2[x];
m=w+32;
cout<<char(m);
}


with in this case, only w = string[x] is the statement under the if condition,
1
2
m=w+32;
cout<<char(m);
should execute always...
Last edited on
Topic archived. No new replies allowed.