Quantcast

Maximum PC

It is currently Sat Apr 19, 2014 2:16 am

All times are UTC - 8 hours




Post new topic Reply to topic  [ 34 posts ]  Go to page Previous  1, 2
Author Message
 Post subject:
PostPosted: Tue Dec 09, 2008 6:33 pm 
Little Foot
Little Foot

Joined: Sat Feb 11, 2006 11:04 am
Posts: 174
never mind guys i found out how they work:

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

int main()
{



char a[20];
cout<<"enter a string: ";
cin.get(a, 20);
cout<<a<<endl;
return 0;
}


Top
  Profile  
 
 Post subject:
PostPosted: Tue Dec 09, 2008 6:42 pm 
Little Foot
Little Foot

Joined: Sat Feb 11, 2006 11:04 am
Posts: 174
wow i actually got it. God dam im lazy all i needed was to read the god dam book. Thank you guys for helping me out.

#include <iostream>
#include <string>
#include <cctype>

using namespace std;

int main()
{



char a[6];
cout<<"enter a string: ";
cin.get(a, 6);
cout<<a<<endl;
for (int i=0; i<6; i++)

a[i] = toupper(a[i]);
cout<<a<<endl;

return 0;
}


Top
  Profile  
 
 Post subject:
PostPosted: Tue Dec 09, 2008 8:53 pm 
Little Foot
Little Foot

Joined: Sat Feb 11, 2006 11:04 am
Posts: 174
im getting this error when i run my program:

stringProgram (extra credit).obj : error LNK2028: unresolved token (0A00028A) "void __cdecl printThird(char * const)" (?printThird@@$$FYAXQAD@Z) referenced in function "int __cdecl main(void)" (?main@@$$HYAHXZ)
stringProgram (extra credit).obj : error LNK2028: unresolved token (0A000296) "void __cdecl reverseString(char * const)" (?reverseString@@$$FYAXQAD@Z) referenced in function "int __cdecl main(void)" (?main@@$$HYAHXZ)
stringProgram (extra credit).obj : error LNK2019: unresolved external symbol "void __cdecl printThird(char * const)" (?printThird@@$$FYAXQAD@Z) referenced in function "int __cdecl main(void)" (?main@@$$HYAHXZ)
stringProgram (extra credit).obj : error LNK2019: unresolved external symbol "void __cdecl reverseString(char * const)" (?reverseString@@$$FYAXQAD@Z) referenced in function "int __cdecl main(void)" (?main@@$$HYAHXZ)


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

using namespace std;

int main()
{
   int choice;
   char a[20];
   cout<<"enter a string: ";
   cin.get(a, 20);
   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;
   cout<<"\n"<<"Enter a choice: ";
   cin>> choice;
   switch (choice)
         {
         case 1:
            convertToupper(a);
            break;
         case 2:
            convertTolower(a);
            break;
         case 3:
            printString(a);
            break;
         case 4:
            reverseString(a);
            break;
         case 5:
            printThird(a);
            break;
         case 6:
            exit(0);
            break;
 
}
   }while(choice !=6);
   return 0;
}


void convertToupper(char arr[])
{
   for (int i=0; i<20; i++)
      arr[i] = toupper(arr[i]);
      
}
void convertTolower(char arr[])
{
   for (int i=0; i<20; i++)
      arr[i] = tolower(arr[i]);
}
void printString(char arr[])
{
   cout<<arr<<endl;
}


Top
  Profile  
 
 Post subject:
PostPosted: Tue Dec 09, 2008 10:16 pm 
Little Foot
Little Foot
User avatar

Joined: Mon Jul 28, 2008 1:39 pm
Posts: 123
By saying char a[6], you are defining an array of chars of length 6 (btw, this is how the string class stores strings internally). Say the user enters "pneumonoultramicroscopicsilicovolcanoconiosis". This is 45 letters, which is a lot more than six. Therefore, you can't put a string into a fixed size array of chars without causing size problems. After all, the end user is the most unpredictable part of programming :)


Top
  Profile  
 
 Post subject:
PostPosted: Wed Dec 10, 2008 9:07 am 
Little Foot
Little Foot

Joined: Sat Feb 11, 2006 11:04 am
Posts: 174
Quertior wrote:
By saying char a[6], you are defining an array of chars of length 6 (btw, this is how the string class stores strings internally). Say the user enters "pneumonoultramicroscopicsilicovolcanoconiosis". This is 45 letters, which is a lot more than six. Therefore, you can't put a string into a fixed size array of chars without causing size problems. After all, the end user is the most unpredictable part of programming :)


SO how would i go about that? Do i leave the subscript blank?


Top
  Profile  
 
 Post subject:
PostPosted: Wed Dec 10, 2008 10:50 am 
Java Junkie
Java Junkie
User avatar

Joined: Mon Jun 14, 2004 10:23 am
Posts: 24218
Location: Granite Heaven
If you want a string, use a string.

A string is a special char array. When you create an array, you must define its size before you use it. If you want to increase the size of your array, you have to create a new array of the new size and copy the contents of your old array into it.

A string is a mutable array. If you create a string and initialise it with the value 'bob', you have create a char array with 3 spaces. If you then change the value of the string to 'cheese', the string is automatically resized to hold the new value without you having to do anything. That's why we say that a string is 'mostly' a char array. Think of it as a char array on steroids ... it does the heavy lifting for you.

It is important for you to understand the difference between a string (an array of characters) and an array of strings (an array of char arrays). The latter is a two-dimensional array.

To fix your problem, you need to use a string where you are using a char[].


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

Joined: Mon Jul 28, 2008 1:39 pm
Posts: 123
He nailed it again. It would behoove you to use strings in this application instead of char arrays, which are all but useless in the presence of strings...


Top
  Profile  
 
 Post subject:
PostPosted: Thu Dec 11, 2008 4:43 am 
SON OF A GUN
SON OF A GUN
User avatar

Joined: Mon Nov 01, 2004 5:41 am
Posts: 11605
Quertior wrote:
He nailed it again. It would behoove you to use strings in this application instead of char arrays, which are all but useless in the presence of strings...
Uhm, what?

I can't help but think this is a terribly inaccurate statement. Chars and char arrays probably get used far more often than actual strings do in most C/C++ that I have seen. I could be wrong, but my experience shows me otherwise.

In his application, I agree, strings are the best way to go. If you mean that the char arrays were "useless" in this scenario, then that is "okay" (although calling anything in programming useless is probably not totally correct... unless you are talking about GOTO :) )


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

Joined: Mon Jun 14, 2004 10:23 am
Posts: 24218
Location: Granite Heaven
I think he meant that it is best to use a string where possible rather than a char[].

As opposed to saying 'characters arrays are useless' which is, of course, kinda silly.


Top
  Profile  
 
 Post subject:
PostPosted: Thu Dec 11, 2008 5:34 am 
SON OF A GUN
SON OF A GUN
User avatar

Joined: Mon Nov 01, 2004 5:41 am
Posts: 11605
Jipstyle wrote:
As opposed to saying 'characters arrays are useless' which is, of course, kinda silly.


Without char arrays, there would be no strings :)


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

Joined: Mon Jun 14, 2004 10:23 am
Posts: 24218
Location: Granite Heaven
Without fire, nothing would ever be charred!


Top
  Profile  
 
 Post subject:
PostPosted: Fri Dec 12, 2008 5:51 pm 
Little Foot
Little Foot

Joined: Sat Feb 11, 2006 11:04 am
Posts: 174
Jipstyle wrote:
Without fire, nothing would ever be charred!


I changed it to string a[20] now it says cannot convert parameter 1 from std::string to int


Top
  Profile  
 
 Post subject:
PostPosted: Sat Dec 13, 2008 12:47 pm 
Little Foot
Little Foot
User avatar

Joined: Mon Jul 28, 2008 1:39 pm
Posts: 123
Because the best part about strings as opposed to char arrays is that you don't need to specify a length; the string resizes automatically. Correct syntax:
Code:
string str;
//or
string str("");


Top
  Profile  
 
 Post subject:
PostPosted: Wed Jan 14, 2009 8:34 pm 
Little Foot
Little Foot

Joined: Sat Feb 11, 2006 11:04 am
Posts: 174
I took another "stab" at this problem after finishing the chapter on arrays and strings. Here's my final working code:

Code:
#include <iostream>
#include <string>
#include <cctype>


using namespace std;
string upper(string, int );
string lower(string, int);
string reverse(string, int );
void printstr(string);
void printThird(string);


int main()
{
   string word;
   int size, choice;

   cout<<"Enter a word: ";
   cin>>word;
   size= word.length();
   
    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:
            word= upper(word, size);
            break;
         case 2:
            word= lower(word, size);
         break;
         case 3:
            printstr(word);
            break;
       case 4:
           word= reverse(word, size);
           break;
       case 5:
          printThird(word);
           break;
       case 6:
          exit(0);
         }
           }while(choice < 6);
   
   
   return 0;
}
string upper(string name, int length)
{
   int i;
   for (i=0; i<length; i++)
      name[i]=toupper(name[i]);
   return name;
}
string lower(string name, int length)
{
   int i;
   for( i=0; i<length; i++)
      name[i]=tolower(name[i]);
   
   return name;
}
string reverse(string name, int length)
{
   int i=(length-1);
   for( i; i>=0; i--)
      cout<<name[i];
   return name;
}
void printstr(string name)
{
   cout<<name<<endl;
}
void printThird(string name)
{
   cout<<name[2]<<endl;
}




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

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