LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 05-13-2009, 09:11 PM   #1
iochinome
Member
 
Registered: Mar 2009
Posts: 32

Rep: Reputation: 15
c++ syntax in c in ubuntu: 'string' and 'cin'


hey guys, so i am writing a program in c for ubuntu, but the problem is, i have never really done dev. for linux before. my c++ experience was pretty much limited to dev-c++ on windows. a couple things came up upon compilation of my program, included among them:

-the type 'string'. for some reason, when i try to declare a variable as type string, it denies me. like this:
string variable;
it gives an error message. is there another word for it in unix?

-the command-line user input prompt function: i used
cin >> variable;
just because that seemed reasonable, but it seems that
cout << "hello world!";
is different in unix, too, so what is the function for getting user input? (i want to have them enter a string in the terminal. this is just for testing purposes, so it doesnt have to be fancy)

thanks, much appreciated!
 
Old 05-14-2009, 07:37 AM   #2
johnsfine
LQ Guru
 
Registered: Dec 2007
Distribution: Centos
Posts: 5,286

Rep: Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197
Quote:
Originally Posted by iochinome View Post
i am writing a program in c for ubuntu
Do you mean C or C++? If you mean C, why?

Quote:
string variable;
it gives an error message. is there another word for it in unix?
No. string is part of namespace std in C++. Programming on unix does not change that.

If you are programming in C++ and you include the correct header and you specify the namespace, such as with the line
Code:
using namespace std;
then the compiler will understand
Code:
string variable;
I don't know which of those three things you didn't do.
 
Old 05-14-2009, 08:06 AM   #3
b0uncer
LQ Guru
 
Registered: Aug 2003
Distribution: CentOS, OS X
Posts: 5,131

Rep: Reputation: Disabled
And if you are indeed programming in C (not C++), there is no such thing as "string"---the closest thing you get is an array of chars the size of the "string" you put there. See the various C manuals for more information about this..if you're doing it in C, you probably have your reasons (as opposed to using C++ which has more fancy features like those strings for example), but then again you should get to know the language before trying to do something with it. C is a very little language, and a lot that you've accustomed to in C++ isn't there.

Another thing is that you should never use
Code:
using namespace std;
in a C++ application. It's simply bad practise and works only in very very small apps (i.e. "hello world") where you are aware of the whole code at all times; in any bigger program it's a potential trap.
 
Old 05-14-2009, 12:43 PM   #4
iochinome
Member
 
Registered: Mar 2009
Posts: 32

Original Poster
Rep: Reputation: 15
hmm... thats too bad, no such thing as strings in c? the reason that i am programming this in c is the original code that it is based off of was written in c, i am really just tailoring it to fit my needs. so is there a way to just convert a c program into c++? i feel like that probably would not go that well, but then again i am not so clear on the differences. (other than that c++ clearly has more to it)

so what about gathering user input? cin >> does not work, in c at least.

thanks again, much appreciated!
-j
 
Old 05-14-2009, 01:51 PM   #5
johnsfine
LQ Guru
 
Registered: Dec 2007
Distribution: Centos
Posts: 5,286

Rep: Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197
Usually it is easy to convert C code to C++.

It is also very easy to call C code from C++, so you could leave most of a project in C and still write a few functions in C++

streams (cin>> and cout<< etc.) are C++ features. Those are not in C.

Last edited by johnsfine; 05-14-2009 at 01:52 PM.
 
Old 05-14-2009, 03:38 PM   #6
iochinome
Member
 
Registered: Mar 2009
Posts: 32

Original Poster
Rep: Reputation: 15
really? so there is no way to get user input in c?
 
Old 05-14-2009, 03:57 PM   #7
johnsfine
LQ Guru
 
Registered: Dec 2007
Distribution: Centos
Posts: 5,286

Rep: Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197
Quote:
Originally Posted by iochinome View Post
really? so there is no way to get user input in c?
How does saying there are no streams translate to no way to get user input? Of course you can get user input in C, just not as conveniently as in C++.

There are various forms of "gets" function for reading a line of input and/or you can use ordinary file reading functions to read from stdin. Then you can parse the text that you read.

There are also scanf functions in C that both read input and parse it. Those are seriously flawed in several ways and difficult to learn and a major source of beginner errors in C programming. Personally, I would stay away from the whole family of scanf functions and use more basic file I/O operations on stdin, followed by parsing.

But since you know some C++ and know how to use streams, it would be easiest for you to mix C and C++. Write any new functions that get user input in C++.
 
Old 05-14-2009, 07:21 PM   #8
iochinome
Member
 
Registered: Mar 2009
Posts: 32

Original Poster
Rep: Reputation: 15
so when you say 'mix,' what exactly are you thinking? just converting the whole file to a .cpp extension, adding in some c++ code and compiling it?
 
Old 05-15-2009, 07:19 AM   #9
johnsfine
LQ Guru
 
Registered: Dec 2007
Distribution: Centos
Posts: 5,286

Rep: Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197
Quote:
Originally Posted by iochinome View Post
so when you say 'mix,' what exactly are you thinking?
"Mix" would mean having more than one source file, with C code in a .c file and C++ code in a .cpp file, and using the extern "C" feature of C++ to declare both C functions to be called from C++ code and C++ functions to be called from C code.

Quote:
just converting the whole file to a .cpp extension, adding in some c++ code and compiling it?
That's an alternate approach. Change the extension to .cpp and try compiling the existing C code as C++. Fix whatever details go wrong (hopefully not too many) then you can put new C++ code anywhere in the resulting file.
 
Old 05-15-2009, 07:58 AM   #10
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Mint
Posts: 17,809

Rep: Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743
Moved: This thread is more suitable in <Programming> and has been moved accordingly to help your thread/question get the exposure it deserves.
 
Old 05-15-2009, 03:40 PM   #11
iochinome
Member
 
Registered: Mar 2009
Posts: 32

Original Poster
Rep: Reputation: 15
ok, so i tried just switching the file extension name from .c to .cpp and three errors came up, i do not really know how to fix:

myprogram.cpp: In function ‘void init_read(unsigned int)’:
myprogram.cpp:326: error: invalid conversion from ‘void*’ to ‘buffer*’

here is the definition of buffer:
Code:
struct buffer 
{
        void *                  start;
        size_t                  length;
};

struct buffer *         buffers         = NULL;
then here is the actual function void init_read(unsigned int)
Code:
static void
init_read                       (unsigned int           buffer_size)
{
        buffers = calloc (1, sizeof (*buffers));

        if (!buffers) {
                fprintf (stderr, "Out of memory\n");
                exit (EXIT_FAILURE);
        }

        buffers[0].length = buffer_size;
        buffers[0].start = malloc (buffer_size);

        if (!buffers[0].start) {
                fprintf (stderr, "Out of memory\n");
                exit (EXIT_FAILURE);
        }
}
the error occurs on that line starting with 'buffers = calloc...'

why does this work in c and not c++?
thanks, much appreciated!
 
Old 05-15-2009, 03:51 PM   #12
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
Quote:
Originally Posted by iochinome View Post
ok, so i tried just switching the file extension name from .c to .cpp and three errors came up, i do not really know how to fix:

myprogram.cpp: In function ‘void init_read(unsigned int)’:
myprogram.cpp:326: error: invalid conversion from ‘void*’ to ‘buffer*’

here is the definition of buffer:
Code:
struct buffer 
{
        void *                  start;
        size_t                  length;
};

struct buffer *         buffers         = NULL;
then here is the actual function void init_read(unsigned int)
Code:
static void
init_read                       (unsigned int           buffer_size)
{
        buffers = calloc (1, sizeof (*buffers));

        if (!buffers) {
                fprintf (stderr, "Out of memory\n");
                exit (EXIT_FAILURE);
        }

        buffers[0].length = buffer_size;
        buffers[0].start = malloc (buffer_size);

        if (!buffers[0].start) {
                fprintf (stderr, "Out of memory\n");
                exit (EXIT_FAILURE);
        }
}
the error occurs on that line starting with 'buffers = calloc...'

why does this work in c and not c++?
thanks, much appreciated!

Publish you code with line numbers - why should we guess where exactly the compiler complains ?

You can use

Code:
cat -n infile > outfile
in order to get line numbers.
 
Old 05-15-2009, 03:53 PM   #13
johnsfine
LQ Guru
 
Registered: Dec 2007
Distribution: Centos
Posts: 5,286

Rep: Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197
Quote:
Originally Posted by iochinome View Post
i tried just switching the file extension name from .c to .cpp and three errors came up
That's pretty good. Hopefully (and probably) all the errors are compile time, not run time, so you are almost there.

Quote:
invalid conversion from ‘void*’ to ‘buffer*’
That is one of the rules of C++ that is different from C. In C++ you need an explicit cast to convert from void* to another pointer type. If the original C code was correct, then the error message even tells you the type you need to cast to.

So change the line from
Code:
        buffers = calloc (1, sizeof (*buffers));
to
Code:
        buffers = (buffer*)calloc (1, sizeof (*buffers));
 
Old 05-15-2009, 03:57 PM   #14
paulsm4
LQ Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
Argh!

C is not C++ - not on any platform!
Code:
  // This is C++ ... but you tried compiling it as a C program
  std::string mystring;
  cin >> mystring;
  cout << "hello world!";
Code:
  /* This is C-ish .. but you've put it in a .cpp file */ 
  static void
  init_read (unsigned int buffer_size)
  {
    buffers = calloc (1, sizeof (*buffers));

    if (!buffers) {
      fprintf (stderr, "Out of memory\n");
      exit (EXIT_FAILURE);
    }

    buffers[0].length = buffer_size;
    buffers[0].start = malloc (buffer_size);

    if (!buffers[0].start) {
      fprintf (stderr, "Out of memory\n");
      exit (EXIT_FAILURE);
    }
  }
 
Old 05-18-2009, 10:42 PM   #15
iochinome
Member
 
Registered: Mar 2009
Posts: 32

Original Poster
Rep: Reputation: 15
thanks for all the info guys, i really appreciate it. i am still running in to a few issues though, and since you have been so helpful so far, i thought i might bring it up:

so i decided that i would program this in c because of the other things it will need to interface with, and i just feel more comfortable with it. one thing i am not so clear on is strings of characters. i am modifying someone else's code, mind you, so this is not all code that i just randomly wrote. so take a look at this:

i define 'dev_name' like so:
Code:
static char *           dev_name        = NULL;                     //will hold the filepath to a device file
then later on, in the 'main', i assign it like this:
Code:
       dev_name = "/dev/video0";
now for some reason, C++ does not allow me to make a string of characters like that. the error it gives me is
"warning: deprecated conversion from string constant to 'char*'"

how do i get around this? it is somewhat important to the program that this remains as a string of characters, as opposed to a std::string- it gets called by stat() and fprintf() and the like a few times, and they are not too friendly from what i can tell.

well thanks, i really do appreciate it!
-j
 
  


Reply



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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
xemacs syntax highlighting only works on string literals! (Slackware) turbo_spool Linux - Software 3 11-22-2007 10:25 AM
how to use vim has color syntax on ubuntu pk_kala Ubuntu 4 09-08-2007 04:17 PM
syntax error in string trim and wrong node routing agent in tcl script newbie06 Linux - General 0 02-23-2007 02:00 AM
need snmp syntax to change a community string RedhatES4_user Linux - Software 1 02-12-2007 06:47 AM
How to cin >> string? kornerr Programming 2 11-01-2005 03:58 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 10:53 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration