Fedora Linux Support Community & Resources Center
  #1  
Old 13th February 2007, 02:34 PM
teishu's Avatar
teishu Offline
Registered User
 
Join Date: Aug 2006
Location: 127.0.0.1
Age: 22
Posts: 134
Char array problem in c++

Hi, im struggling with character arrays in c++.
I need to make an array containing what the user enters, here is what i have :

Code:
cin >> char word[];
  cout <<word << endl;
i don't know the size of the array until they have entered the string.
__________________
AMD Athlon 2500+ Barton @ 2.2Ghz [11.0*200mhz] -- AsRock K7S41GX -- 1280MB Ram -- Windows XP PRO SP2
AMD Sempron 2400+ -- XPC SK43G -- 512mb Ram -- Fedora 7 -- Registered Linux System: 348507
Linux user number 441738
ASCII stupid question, get a stupid ANSI !
Reply With Quote
  #2  
Old 13th February 2007, 03:07 PM
teishu's Avatar
teishu Offline
Registered User
 
Join Date: Aug 2006
Location: 127.0.0.1
Age: 22
Posts: 134
here is what i have,
Code:
int main(int argc, char *argv[])
{
int size = 0;
cout << "Ascii Converter" << endl;
cout << "By Jason Underhill" << endl;
cout << "-----------------------" << endl;
cout << "Enter the string to be converted to ASCII:" << endl;
string word;
cin >> word;
size = strlen(word);
char w[] = word;

  
  

  return EXIT_SUCCESS;
}
and here are the errors i get:

/home/teishu/C++/ascii_converter/src/ascii_converter.cpp:41: error: cannot convert ‘std::string’ to ‘const char*’ for argument ‘1’ to ‘size_t strlen(const char*)’
/home/teishu/C++/ascii_converter/src/ascii_converter.cpp:42: error: initializer fails to determine size of ‘w’
__________________
AMD Athlon 2500+ Barton @ 2.2Ghz [11.0*200mhz] -- AsRock K7S41GX -- 1280MB Ram -- Windows XP PRO SP2
AMD Sempron 2400+ -- XPC SK43G -- 512mb Ram -- Fedora 7 -- Registered Linux System: 348507
Linux user number 441738
ASCII stupid question, get a stupid ANSI !
Reply With Quote
  #3  
Old 13th February 2007, 03:57 PM
teishu's Avatar
teishu Offline
Registered User
 
Join Date: Aug 2006
Location: 127.0.0.1
Age: 22
Posts: 134
even with this i get one error:

Code:
int main(int argc, char *argv[])
{
int size = 0;
cout << "Ascii Converter" << endl;
cout << "By Jason Underhill" << endl;
cout << "-----------------------" << endl;
cout << "Enter the string to be converted to ASCII:" << endl;
string word;
cin >> word;
size = word.length() - 1;
char wrdar[size] = word;

  
  

  return EXIT_SUCCESS;
}
/home/teishu/C++/ascii_converter/src/ascii_converter.cpp:42: error: variable-sized object ‘wrdar’ may not be initialized
__________________
AMD Athlon 2500+ Barton @ 2.2Ghz [11.0*200mhz] -- AsRock K7S41GX -- 1280MB Ram -- Windows XP PRO SP2
AMD Sempron 2400+ -- XPC SK43G -- 512mb Ram -- Fedora 7 -- Registered Linux System: 348507
Linux user number 441738
ASCII stupid question, get a stupid ANSI !
Reply With Quote
  #4  
Old 13th February 2007, 04:35 PM
InfRecursion Offline
Registered User
 
Join Date: Jan 2005
Posts: 214
Try this:

Code:
#include <iostream>

using namespace std;


int main(int argc, char *argv[])
{
int size = 0;
cout << "Ascii Converter" << endl;
cout << "By Jason Underhill" << endl;
cout << "-----------------------" << endl;
cout << "Enter the string to be converted to ASCII:" << endl;
string word;
cin >> word;
size = word.length();
char w[size];
strcpy(w,word.c_str());

cout << "W: " << w << endl;

  return EXIT_SUCCESS;
}
Reply With Quote
  #5  
Old 13th February 2007, 04:45 PM
teishu's Avatar
teishu Offline
Registered User
 
Join Date: Aug 2006
Location: 127.0.0.1
Age: 22
Posts: 134
cheers m8, seems to work ok
__________________
AMD Athlon 2500+ Barton @ 2.2Ghz [11.0*200mhz] -- AsRock K7S41GX -- 1280MB Ram -- Windows XP PRO SP2
AMD Sempron 2400+ -- XPC SK43G -- 512mb Ram -- Fedora 7 -- Registered Linux System: 348507
Linux user number 441738
ASCII stupid question, get a stupid ANSI !
Reply With Quote
  #6  
Old 13th February 2007, 06:15 PM
InfRecursion Offline
Registered User
 
Join Date: Jan 2005
Posts: 214
Glad I could help
Reply With Quote
  #7  
Old 13th February 2007, 06:21 PM
teishu's Avatar
teishu Offline
Registered User
 
Join Date: Aug 2006
Location: 127.0.0.1
Age: 22
Posts: 134
Quote:
Originally Posted by InfRecursion
Glad I could help
how long have you been programming ?
__________________
AMD Athlon 2500+ Barton @ 2.2Ghz [11.0*200mhz] -- AsRock K7S41GX -- 1280MB Ram -- Windows XP PRO SP2
AMD Sempron 2400+ -- XPC SK43G -- 512mb Ram -- Fedora 7 -- Registered Linux System: 348507
Linux user number 441738
ASCII stupid question, get a stupid ANSI !
Reply With Quote
  #8  
Old 14th February 2007, 01:52 PM
InfRecursion Offline
Registered User
 
Join Date: Jan 2005
Posts: 214
Quote:
Originally Posted by teishu
how long have you been programming ?
around 17 years or so, professionally for 9.
Reply With Quote
  #9  
Old 14th February 2007, 02:47 PM
teishu's Avatar
teishu Offline
Registered User
 
Join Date: Aug 2006
Location: 127.0.0.1
Age: 22
Posts: 134
ah.. thats why your so good :P
__________________
AMD Athlon 2500+ Barton @ 2.2Ghz [11.0*200mhz] -- AsRock K7S41GX -- 1280MB Ram -- Windows XP PRO SP2
AMD Sempron 2400+ -- XPC SK43G -- 512mb Ram -- Fedora 7 -- Registered Linux System: 348507
Linux user number 441738
ASCII stupid question, get a stupid ANSI !
Reply With Quote
  #10  
Old 14th February 2007, 07:33 PM
InfRecursion Offline
Registered User
 
Join Date: Jan 2005
Posts: 214
Nah. I think I'm pretty rusty actually... There are several people here and elsewhere whom are better. After all the years, it gets easier to spot the errors. Lots of trial-and-error.
Reply With Quote
  #11  
Old 16th February 2007, 12:00 AM
essence Offline
Registered User
 
Join Date: Jan 2007
Posts: 19
quick question, are you using turbo c++ ?
Reply With Quote
  #12  
Old 16th February 2007, 12:27 AM
teishu's Avatar
teishu Offline
Registered User
 
Join Date: Aug 2006
Location: 127.0.0.1
Age: 22
Posts: 134
no im using Kdev under fedora core 6
__________________
AMD Athlon 2500+ Barton @ 2.2Ghz [11.0*200mhz] -- AsRock K7S41GX -- 1280MB Ram -- Windows XP PRO SP2
AMD Sempron 2400+ -- XPC SK43G -- 512mb Ram -- Fedora 7 -- Registered Linux System: 348507
Linux user number 441738
ASCII stupid question, get a stupid ANSI !
Reply With Quote
Reply

Tags
array, char, problem

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Problem with HP Smart Array 642 controller Sirc Hardware & Laptops 1 11th February 2008 12:57 PM
C++ deallocation of a char**? keeler1 Programming & Packaging 2 3rd March 2007 03:17 PM
intel D945 matrix raid 1 array how do i rebuild array? cow Hardware & Laptops 4 13th April 2006 03:06 PM
Problem disk array (cluster) manstt Servers & Networking 1 22nd November 2005 07:19 AM
How do I set Tab key to be 3-char indent instead of 6-char indent? TheProgrammer Using Fedora 3 17th September 2005 08:55 AM


Current GMT-time: 07:27 (Tuesday, 21-05-2013)

TopSubscribe to XML RSS for all Threads in all ForumsFedoraForumDotOrg Archive
logo

All trademarks, and forum posts in this site are property of their respective owner(s).
FedoraForum.org is privately owned and is not directly sponsored by the Fedora Project or Red Hat, Inc.

Privacy Policy | Term of Use | Posting Guidelines | Archive | Contact Us | Founding Members

Powered by vBulletin® Copyright ©2000 - 2012, vBulletin Solutions, Inc.

FedoraForum is Powered by RedHat