need help with my loop

Hi I have a minor(I think it is minor) problem with one of my loop. the loop is supposed to uppercase every letters in the word enter but it stop after one letter.

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
#include <iostream>
#include <stdlib.h>
using namespace std;

inline char Upper(char a[256]){
int i = 0;
	do {
		
	  return toupper(a[i]); 
	} while (a[i] = getchar() != '.');
}
int main (){
	char word[256],rep;
	
	cout << "please enter u (for upper) or l (for lower)" << endl;
	cin >> rep;
	
	if (rep = 'u'){
	cout << "enter the word"<<endl;
	cin >> word;
	cout << "the word in uppercase :" << Upper(word) << endl;
	}
	
	return 0;
	
}



thanks in advance.
Last edited on
A function can return only once.
That is why only the first letter is being converted by Upper .

ahhhhhhh!! thanks!!! i will put the return outside of the loop. thanks a lot.

edit

now this is the changed program and now whatever i write the "cout" is a heart not the the word in uppercase

#include <iostream>
#include <stdlib.h>
using namespace std;

inline char Upper(char a[256]){
int i = 0;
	do {
	 toupper(a[i]); 
	} while (a[i] = getchar() != '.');
return a[256];
}
int main (){
	char word[256],rep;
	
	cout << "please enter u (for upper) or l (for lower)" << endl;
	cin >> rep;
	
	if (rep = 'u'){
	cout << "enter the word"<<endl;
	cin >> word;
	cout << "the word in uppercase :" << Upper(word) << endl;
	}
	
	return 0;
	
}
Last edited on
Your Upper is all messed up.

When passing a 1D array, you dont need to specify the size.
You return a[256] which does not contain any valid data.
Even if it did, you will still see only 1 character in the output coz as i said, a function can return only once. And you choosed to return something invalid.

Fix it like this:

Declare the function as void. It does not have to return anything.
inline void Upper(char a[]) .

I dont want to upset your reasoning about using do..while and getchar() .

Lastly, remove return a[256]

mmmmm I see, but by saying "I dont want to upset your reasoning about using do..while and getchar() ."
do you mean that I have to change the do..while? because I'm trying to uppercase every letters enter until "." is enter, so I think I need it.

thanks for the answer, I will try to work on what you have told me, if I can't make it work I will try something different.
thanks again.
Last edited on
Topic archived. No new replies allowed.