lower case string into upper case string

Sep 7, 2011 at 8:02am
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 Sep 7, 2011 at 9:01am
Sep 7, 2011 at 8:51am
0 > name.length();

When do you expect zero to be greater than the length of the name?
Sep 7, 2011 at 8:52am
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.
Sep 7, 2011 at 8:55am
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 Sep 7, 2011 at 9:00am
Sep 7, 2011 at 8:59am
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 Sep 7, 2011 at 9:04am
Sep 7, 2011 at 9:02am
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 Sep 7, 2011 at 9:02am
Topic archived. No new replies allowed.