-1

this is a program to change the case of all letters input by the user to small. im getting the right output with this coding. the problem is that its giving some staircase type structure as the output as well. HOW DO I GET RID OF THIS EXTRA STUFF?

#include <iostream>
using namespace std;
void main ()
{
	char word [20];
	cout<<"enter line :";
	cin.getline(word,20);
	int wordint [20];
	char wordchar [20];
	for (int i=0; i<20; i++)
	{
		wordint[i]=(int)word[i];
	if(wordint[i]<=90)
		wordint[i]+=32;
	wordchar[i]=(char)wordint[i];
if(word[i]=='\0')
		break;
	}
	cout<<wordchar;
	
	
}

Edited by peter_budo: Keep It Organized - For easy readability, always wrap programming code within posts in [code] (code blocks)

Votes + Comments
If you are going to ask a question then provide all of the information and use [code][/code] tags
4
Contributors
10
Replies
11
Views
7 Years
Discussion Span
Last Post by maryam ahmad
0

The reason you get the 'messy' output is that you add 32 to all char's with a value less than, or equal to, 90.

That works just fine for the alphabet but when you reach the 'string terminator' ('/0') things go bad. /0 simply has the value 0.

You end your for-loop by adding 32 to the value of the 'string terminator'. Effectively replacing it with a space (' ').

You should make sure that you only add 32 to the chars that are part of the alphabet.

The char's from A to Z have the values 65 to 90.

Change your 'if' from:

if(wordint[i]<=90)

to

if(wordint[i] >= 65 && wordint[i]<=90)

and it should work

0

Try entering stuff like "1234567890." into your original program, and you will see the problem

-1

In addition to what has been suggested above, I would advise you to run the for loop from 0.....strlen(word) as opposed to 0....20

0

i tried the if statement with 65 to 90. it doesnt give messy stuff with spaces and punctuations but still gives staircase kinda thing at the end.
And about the strlen(word)... we arnt allowed to use sring.h for this program :(

0

You have not put a '\0' in your wordchar string.
So you get the correct input and then a bunch of garbage. So you want to put a '\0' in the wordchar string after the correct input ends.

0

Thats kind of wierd. The garbage-output is caused by a missing '\0'.

If there is a '\0' in 'word' it should definately be copied to 'wordchar' and it works on my system.

Are you sure you haven't made changes to the source, after posting the original question?

This code works on my computer:

#include <iostream>
using namespace std;
void main ()
{
	char word [20];
	cout<<"enter line :";
	cin.getline(word,20);
	int wordint [20];
	char wordchar [20];
	for (int i=0; i<20; i++)
	{
		wordint[i]=(int)word[i];
		if(wordint[i]>=65 && wordint[i]<=90)
			wordint[i]+=32;
		wordchar[i]=(char)wordint[i];
		if(word[i]=='\0')
			break;
	}
	cout<<wordchar;
}

Only change is in the if-statement.

Edited by mbulow: n/a

0

If this does not work...

Could you please try to output 'word' right after cin.getline(word, 20) to make sure the input contains the '\0'.

0

yes, it was that 'if' statement creating troubles.. with 65-90 interval it works perfectly.. THANKS AGAIN

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.