6 Replies - 65141 Views - Last Post: 13 April 2006 - 04:29 AM Rate Topic: -----

#1 maryj  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 12-April 06

Convert String to Uppercase

Posted 12 April 2006 - 10:23 AM

im new to C++, im to write a program where the the given string converts itself to uppercase. I think i have most of the code, but dont know why its not printing the caps. It ends up printing with "The charecters in caps are:"
and thats it, it dosnt really give me the sentence following that. My code:
#include <iostream>
#include <string>
using namespace std;

int main()

 {
  int i;
  string str;
  
cout << "My name is Mike Jordan, and I am 20 years old" <<endl;


for (i = 0; i < str.length(); i++)

{ 
    str[i]=toupper(str[i]);
}
	cout << "The characters in uppercase, are: "
	<< str[i] << endl;
    


cin.ignore();
return 0;
}



any help is appreciated.

mary

Is This A Good Question/Topic? 0
  • +

Replies To: Convert String to Uppercase

#2 Nova Dragoon  Icon User is offline

  • The Innocent Shall Suffer, Big Time
  • member icon

Reputation: 36
  • View blog
  • Posts: 6,169
  • Joined: 16-August 01

Re: Convert String to Uppercase

Posted 12 April 2006 - 10:51 AM

well you never actually fill str with anything
Was This Post Helpful? 0
  • +
  • -

#3 maryj  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 12-April 06

Re: Convert String to Uppercase

Posted 12 April 2006 - 10:53 AM

well thanks for pointing that out for me. I think I was just looking at it too long.

Thanks again
Was This Post Helpful? 0
  • +
  • -

#4 skyhawk133  Icon User is offline

  • Head DIC Head
  • member icon

Reputation: 1877
  • View blog
  • Posts: 20,284
  • Joined: 17-March 01

Re: Convert String to Uppercase

Posted 12 April 2006 - 10:54 AM

You might want to take a look at this code snippet submitted by our very own Amadeus: http://code.dreaminc...ppet.php?sid=39

The problem is you are trying to output str[i] which is just 1 letter in your str array. You'll need to append the upper case letter to a new variable, then output that new variable. I believe Amadeus does this in his snippet, so take a look and let us know if you have any other questions.
Was This Post Helpful? 0
  • +
  • -

#5 Quiksilver  Icon User is offline

  • New D.I.C Head
  • member icon

Reputation: 1
  • View blog
  • Posts: 45
  • Joined: 12-March 05

Re: Convert String to Uppercase

Posted 12 April 2006 - 03:41 PM

the problem is this:

cout << "The characters in uppercase, are: "
<< str[i] << endl;

all your doing is outputting a single character of the string (the last one that was changed in the foor loop. 'i' still equals the last letter in the string.)

you need to use a loop to output the entire string.

something like this :
for(int i=0;i<str.length();i++)
{
       cout<<str[i]<<endl;
}


This post has been edited by Quiksilver: 12 April 2006 - 03:43 PM

Was This Post Helpful? 0
  • +
  • -

#6 Amadeus  Icon User is offline

  • g+ + -o drink whiskey.cpp
  • member icon

Reputation: 248
  • View blog
  • Posts: 13,507
  • Joined: 12-July 02

Re: Convert String to Uppercase

Posted 12 April 2006 - 07:00 PM

Quiksilver, on 12 Apr, 2006 - 04:33 PM, said:

the problem is this:

cout << "The characters in uppercase, are: "
<< str[i] << endl;

While it is true that the user will have to loop through the string to otuput each character, the real problem, as noted by Nova, is that the string variable itself was declared but never has a value assigned to it. Even if the user were to loop through the string (and it would be a loop of zero iterations), the output would be nothing.
Was This Post Helpful? 0
  • +
  • -

#7 born2c0de  Icon User is offline

  • printf("I'm a %XR",195936478);
  • member icon

Reputation: 180
  • View blog
  • Posts: 4,667
  • Joined: 26-November 04

Re: Convert String to Uppercase

Posted 13 April 2006 - 04:29 AM

Use printf for such a statement using the %s format specifier.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1