lower case string into upper case string

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>
#include <string>
#include <ctype.h>

using namespace std;

string capital(string name)
{
	
	for (int i = 0; i < name.length(); i++)
	{
		name[i] = toupper(name[i]);
	}
	
	return name;
	
}

int main()
{
          string name = "robbie";
	  cout << capital(name) << endl;
}


So far I got that code up while trying to make robbie into ROBBIE my problem is not being able to loop correctly to help toupper do its job any help as to what I need to fix
Last edited on
0 > name.length();

When do you expect zero to be greater than the length of the name?
My bad that was clearly a typo it should be i like most for loops are -_- stupid on my part but should have been easily overlooked by you imo.
stupid on my part but should have been easily overlooked by you imo.


I see. I genuinely thought this was your code and that was the mistake you made. I suggest that you copy and paste your code in future so that the code you show us is the code that doesn't work.

i > name.length();

This will be false from the start, so the loop will never be executed. Try

i < name.length();

I thought about ignoring this error because it's clearly a typo, as you suggested, but thought maybe I should point it out anyway.
Last edited on
heh thanks for that one I clearly overlooked that one entirely in my code. I had though I wrote the array wrong but sadly it was just the < that was wrong once again I feel stupid after this thanks for the help and I couldn't really copy paste this since there is more to the program than the small section showed the original is a jumbled mess but I appreciate the help even though it was a small thing
Last edited on
This code works:

http://ideone.com/NaQcy

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>
#include <string>
#include <ctype.h>

using namespace std;

string capital(string name)
{
	
	for (int i = 0; i < name.length(); i++)
	{
		name[i] = toupper(name[i]);
	}
	
	return name;
	
}

int main()
{
          string name = "robbie";
	  cout << capital(name) << endl;
}
 


Last edited on
Topic archived. No new replies allowed.