Quantcast

Maximum PC

It is currently Fri Aug 01, 2014 2:38 am

All times are UTC - 8 hours




Post new topic Reply to topic  [ 34 posts ]  Go to page 1, 2  Next
Author Message
 Post subject: can i get a hand here please
PostPosted: Wed Dec 03, 2008 11:28 am 
Little Foot
Little Foot

Joined: Sat Feb 11, 2006 11:04 am
Posts: 174
This is another hw assaignment.

Write a program that ask user to enter a string. Then write a loop that prompt user options:

1. Convert the string to uppercase
2. convert the string to lowercase
3. Print the string
4. reverse the string
5. print out the third letter of the string
6. exit


Heres my code: (prototypes are on a separate header file)

Code:
#include <iostream>
#include <iomanip>
#include <string>
#include <cctype>
#include "prototypes.h"

using namespace std;
string word;
int choice;
int main()
{
   
   
   cout<<"Enter a word: ";
   cin>> word;

      
   do{
           cout<<"1. Convert the string to uppercase\n"
         <<"2. convert the string to lowercase\n"
         <<"3. Print the string\n"
         <<"4. reverse the string\n"
         <<"5. print out the third letter of the string\n"
         <<"6. exit" <<endl;
            cin>> choice;
         switch (choice)
         {
         case 1:
            convertTOUPPER();
            break;
         case 2:
            convertTOLOWER();
         case 3:
            printSTRING();
            break;
         
         }
           }while(choice < 6);
         return 0;
}
void convertTOUPPER()
{
   for (int i =0; i < word.length(); i++)
   
      word[i] = word[toupper(i)];
   
   menu();
}
void convertTOLOWER(string word)
{
   for (int i =0; i < word.length(); i++)
   
      word[i] = word[toupper(i)];
   menu();
}
void printSTRING()
{
   
   cout<<word[2]<<endl;
   menu();
}
void menu()
{
   do{
           cout<<"1. Convert the string to uppercase\n"
         <<"2. convert the string to lowercase\n"
         <<"3. Print the string\n"
         <<"4. reverse the string\n"
         <<"5. print out the third letter of the string\n"
         <<"6. exit" <<endl;
            cin>> choice;
   }while(choice < 6);
}


Im having trouble using the toupper function to convert a string.


Top
  Profile  
 
 Post subject:
PostPosted: Wed Dec 03, 2008 11:31 am 
Java Junkie
Java Junkie
User avatar

Joined: Mon Jun 14, 2004 10:23 am
Posts: 24222
Location: Granite Heaven
What kind of trouble?


Top
  Profile  
 
 Post subject:
PostPosted: Wed Dec 03, 2008 11:49 am 
SON OF A GUN
SON OF A GUN
User avatar

Joined: Mon Nov 01, 2004 5:41 am
Posts: 11605
Code:
void convertTOUPPER()
{
   for (int i =0; i < word.length(); i++)
   
      word[i] = word[toupper(i)];
   
   menu();
}


How about?

Code:
void convertTOUPPER()
{
   for (int i =0; i < word.length(); i++)
   
      word[i] = toupper(word[i]);
   
   menu();
}


See here


Top
  Profile  
 
 Post subject:
PostPosted: Wed Dec 03, 2008 1:03 pm 
Willamette
Willamette
User avatar

Joined: Fri Jul 06, 2007 9:29 am
Posts: 1447
As Crashtech indicated, you're using the function call incorrectly.

You are coding:
word[i] = word[toupper(i)];

The function call should be in the format:

functionname(value) (performs the functionname function on the value)

What you are doing there with word[toupper(i)] is a bit mangled, but essentially something in the format:

value(functionname) (performs the value on the functionname, which doesn't work too well)

This is why it is failing (incidentally, it affects the toLower call in your code as well).


Top
  Profile  
 
 Post subject:
PostPosted: Wed Dec 03, 2008 3:37 pm 
Little Foot
Little Foot

Joined: Sat Feb 11, 2006 11:04 am
Posts: 174
why doesnt my printSTRING() function work?

void printSTRING()
{

cout<<word[0];

menu();
}

Wouldnt that print out the first element of array word?


Top
  Profile  
 
 Post subject:
PostPosted: Wed Dec 03, 2008 4:19 pm 
Willamette
Willamette
User avatar

Joined: Fri Jul 06, 2007 9:29 am
Posts: 1447
What is it doing for you? I would think it would give the first character of whatever word you keyed in.


Top
  Profile  
 
 Post subject:
PostPosted: Wed Dec 03, 2008 4:22 pm 
Little Foot
Little Foot
User avatar

Joined: Mon Jul 28, 2008 1:39 pm
Posts: 123
But how does it know what "word" is? I do not see word passed a a parameter to the function.


Top
  Profile  
 
 Post subject:
PostPosted: Wed Dec 03, 2008 5:04 pm 
Little Foot
Little Foot

Joined: Sat Feb 11, 2006 11:04 am
Posts: 174
Quertior wrote:
But how does it know what "word" is? I do not see word passed a a parameter to the function.


i put string word; as a global. for example, user inputs "hello" and it assigns it to variable word[1]. word[0] is going to = hello, correct? so if word[0] = hello i want to pass word which is global to the printSTRING function. And when its passed to the function i want it to convert the string to upper case. IIt looks like i cant do it this way: cin>> word;
word = toupper(word);


Top
  Profile  
 
 Post subject:
PostPosted: Wed Dec 03, 2008 5:21 pm 
Little Foot
Little Foot
User avatar

Joined: Mon Jul 28, 2008 1:39 pm
Posts: 123
No. if the user enters "hello" then word (without [] ) = hello.
Remember that the [] is used to refer to individual characters, so word[0] = 'h', word[1] = 'e', etc.


Top
  Profile  
 
 Post subject:
PostPosted: Wed Dec 03, 2008 5:42 pm 
Little Foot
Little Foot

Joined: Sat Feb 11, 2006 11:04 am
Posts: 174
Quertior wrote:
No. if the user enters "hello" then word (without [] ) = hello.
Remember that the [] is used to refer to individual characters, so word[0] = 'h', word[1] = 'e', etc.


no word[0] = hello. its a string.


Top
  Profile  
 
 Post subject:
PostPosted: Wed Dec 03, 2008 6:12 pm 
Little Foot
Little Foot
User avatar

Joined: Mon Jul 28, 2008 1:39 pm
Posts: 123
Yes... in the code posted above (at least), word is most definitely a string and not an array. But you contradicted yourself where you said that word[0] = hello. If word were a String array, then word[0] would equal hello. But since word is only a single string, whatever you do using the [] operator will only return a single character. word[0] = 'h'. Q.E.D.

P.S.: It's confusing how word is a global variable that is used in convertTOUPPER but word is overridden in the function convertTOLOWER.
Additionally, since in convertTOLOWER, you neither return word nor make word a reference parameter, that will not change word at all.


Top
  Profile  
 
 Post subject:
PostPosted: Thu Dec 04, 2008 5:21 am 
Java Junkie
Java Junkie
User avatar

Joined: Mon Jun 14, 2004 10:23 am
Posts: 24222
Location: Granite Heaven
xxmainframexpxx wrote:
Quertior wrote:
No. if the user enters "hello" then word (without [] ) = hello.
Remember that the [] is used to refer to individual characters, so word[0] = 'h', word[1] = 'e', etc.


no word[0] = hello. its a string.


A string is an array of characters. word[0] pulls the first char from the string.

You want an array of Strings if you want to pull out the first word in that fashion.

You also want to stop using global variables. Learn to pass variables to functions.


Top
  Profile  
 
 Post subject:
PostPosted: Thu Dec 04, 2008 10:07 am 
Monkey Federation (Top 10)*
Monkey Federation (Top 10)*
User avatar

Joined: Sun May 22, 2005 8:28 am
Posts: 3673
Location: The Blue Nowhere
Jipstyle wrote:
You also want to stop using global variables. Learn to pass variables to functions.


speaking of global variables.
string var1 = "But they are soooooo easy!"

therefore:

var1[0] = B
var1[1] = u
var1[2] = t
var1[3] =
var1[4] = t
var1[5] = h
var1[6] = e
var1[7] = y
var1[8] =
var1[9] = a
var1[10] = r
var1[11] = e
var1[12] =
var1[13] = s
var1[14] = o
var1[15] = o
var1[16] = o
var1[17] = o
var1[18] = o
var1[19] =
var1[20] = e
var1[21] = a
var1[22] = s
var1[23] = y
var1[24] = !


and var1.length() will equal 25


Top
  Profile  
 
 Post subject:
PostPosted: Sun Dec 07, 2008 5:32 pm 
Little Foot
Little Foot

Joined: Sat Feb 11, 2006 11:04 am
Posts: 174
CrashTECH wrote:
Code:
void convertTOUPPER()
{
   for (int i =0; i < word.length(); i++)
   
      word[i] = word[toupper(i)];
   
   menu();
}


How about?

Code:
void convertTOUPPER()
{
   for (int i =0; i < word.length(); i++)
   
      word[i] = toupper(word[i]);
   
   menu();
}


See here

i did that and it doesnt convert it to a uppercase h.


Top
  Profile  
 
 Post subject:
PostPosted: Sun Dec 07, 2008 5:39 pm 
Little Foot
Little Foot

Joined: Sat Feb 11, 2006 11:04 am
Posts: 174
and how come when i do this:

#include <iostream>
#include <cctype>
#include <string>
using namespace std;

int main()
{
string word;
word = "hello";
string a[5];
a[0] =word;
cout<<a[0]<<endl;

return 0;
}

it prints out hello.


Top
  Profile  
 
 Post subject:
PostPosted: Sun Dec 07, 2008 5:44 pm 
Little Foot
Little Foot
User avatar

Joined: Mon Jul 28, 2008 1:39 pm
Posts: 123
xxmainframexpxx wrote:
and how come when i do this:

#include <iostream>
#include <cctype>
#include <string>
using namespace std;

int main()
{
string word;
word = "hello";
string a[5];
a[0] =word;
cout<<a[0]<<endl;

return 0;
}

it prints out hello.

Because the statement string a[5] creates an array of strings with 5 slots numbered 0-4. This means that you can allocate word to a[0] and print it without any problems.


Top
  Profile  
 
 Post subject:
PostPosted: Mon Dec 08, 2008 8:14 am 
Little Foot
Little Foot

Joined: Sat Feb 11, 2006 11:04 am
Posts: 174
Quertior wrote:
xxmainframexpxx wrote:
and how come when i do this:

#include <iostream>
#include <cctype>
#include <string>
using namespace std;

int main()
{
string word;
word = "hello";
string a[5];
a[0] =word;
cout<<a[0]<<endl;

return 0;
}

it prints out hello.

Because the statement string a[5] creates an array of strings with 5 slots numbered 0-4. This means that you can allocate word to a[0] and print it without any problems.

ok then, so why you said earlier that a[0] would be 'h'?


Top
  Profile  
 
 Post subject:
PostPosted: Mon Dec 08, 2008 8:39 am 
Java Junkie
Java Junkie
User avatar

Joined: Mon Jun 14, 2004 10:23 am
Posts: 24222
Location: Granite Heaven
A string is an array of chars (characters).

Quertior defined a string variable called 'word' and assign it the value 'hello', then the string looks like this:

word[0]='h'
word[1]='e'
word[2]='l'
word[3]='l'
word[4]='o'

He then defined an array of 5 strings called 'a' ( string a[5]; ). He assigned the string in the first position of the array ( a[0] ) the same value as the string variable 'word' ( a[0] = word; ).

At this point, a[] looks like this:

a[0]='hello';
a[1]='';
a[2]='';
a[3]='';
a[4]='';

When he said a[0] would be 'h', he was referring to your code, not his.


Top
  Profile  
 
 Post subject:
PostPosted: Mon Dec 08, 2008 4:05 pm 
Little Foot
Little Foot
User avatar

Joined: Mon Jul 28, 2008 1:39 pm
Posts: 123
He nailed it.


Top
  Profile  
 
 Post subject:
PostPosted: Tue Dec 09, 2008 5:15 pm 
Little Foot
Little Foot

Joined: Sat Feb 11, 2006 11:04 am
Posts: 174
i dont get it. Wy doesnt this work:

#include <iostream>
#include <string>

using namespace std;

Code:
int main()
{
     string word;
     cout<<"enter word: ";
cin>> word;
char a[6] = word;
cout<<a[0]<<endl;
return 0;
)


Top
  Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 34 posts ]  Go to page 1, 2  Next

All times are UTC - 8 hours


Who is online

Users browsing this forum: No registered users and 2 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group