473,399 Members | 3,302 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,399 software developers and data experts.

Adding a string or int to a character array

5
Hi,

I have a problem that's (probably) very easy to solve, only I'am more a Java programmer and I couldn't find anything like it on the internet.
My problem is that I have to add a variable (int, string, double) to a character array. I know that it is very easy with Java but I din't find any thing for C/C++.
Everything I found created errors.

Example:
I've added the first 4 lines of the methode "setOffset(int arg1)". "msg" is the char array created at the beginning of the methode it's importend that the size of this array is equal to the size of the hardcoded string + the length of arg1, because it is used in the "viWrite(.....)".
The char array won't be changed after it's made.

Expand|Select|Wrap|Line Numbers
  1. bool Tektronix::setOffset(int arg1){
  2.     char msg[] = "source1:Voltage:Offset " + arg1; //this is the Java way
  3.     status = viWrite(vi, (ViBuf) msg , sizeof(msg), &retCnt);
  4.     if (status < VI_SUCCESS) return false;
  5.         .............
  6.  
ERROR: cannot convert from 'System::String ^' to 'char []'
ERROR: 'char []': illegal sizeof operand

Second problem is that it says illegal sizeof opperand, but I expect that error will disapear if I've solved the first.

Thanks for all the support

GreetZ

Spikey
Oct 2 '07 #1
6 5399
sicarie
4,677 Expert Mod 4TB
I believe you will need string streams. That should help you convert to a string.
Oct 2 '07 #2
Spikey
5
Hi,

I guess it's not that easy... the strcpy I have to use (to convert the string to a char array) is creating a Warning. I can live with that but it's not pretty.
The real problem is that the sstream is creating an error in combination with my errorhandler.

ERROR: error C2362: initialization of 's2' is skipped by 'goto errorHandler'

GreetZ.
Oct 2 '07 #3
Spikey
5
Hi,

I have a few things working now, but I really need to have an array with the exact size of the string.
I was hoping the following code would do the trick, but it ain't

example:
Expand|Select|Wrap|Line Numbers
  1. string s1 = "output1?";  // s1.size = 8
  2. char ch1[s1.size()]; // this ain't gonne work, I know!
  3. strcpy (ch1 , s1.c_str());
  4.  
it's essential that afterwards sizeof(ch1) is equal to the number of characters inside, otherwise I can't use the viWrite methode (created by National Instuments).

every possible help is welcome :D

Greetz

Spikey
Oct 2 '07 #4
weaknessforcats
9,208 Expert Mod 8TB
string s1 = "output1?"; // s1.size = 8
char ch1[s1.size()]; // this ain't gonne work, I know!
strcpy (ch1 , s1.c_str());
Did you read what Sicarie said in Post #2?? He gave you the answer.
This is C++ and not C of 1972. That is, you don't use the C-string library anymore.

Expand|Select|Wrap|Line Numbers
  1. //Java:
  2. char msg[] = "source1:Voltage:Offset " + arg1; //this is the Java way
  3. //C++:
  4. stringstream ss;
  5. ss << "source1:Voltage:Offset " << arg1; 
  6. string msg;
  7. ss >> msg;
  8.  
Oct 2 '07 #5
Spikey
5
Sorry to bother you, but I really NEED a char array like "char msg[]" instead of a string. The only reason I used a string was to add data to a text line, afterwards I really need to convert it to a char array.
The methode viWrite is a methode writen by National Instruments and I only can convert a char array to a ViBuf (see also code of post#1). ViBuf is defined as a char.

in the visatype.h (the source of National Instruments) is the following defined about ViBuf
Expand|Select|Wrap|Line Numbers
  1. typedef unsigned char       ViByte;
  2. typedef ViByte      _VI_PTR ViPByte;
  3. typedef ViPByte             ViBuf;
  4.  
So please if some body understand what I am trying to say (what i need) and know a way to solve the problem. please help, but read Post#1 and Post#4 really carefully before you reply and also come with a solution ending with a string!

GreetZ.
Oct 3 '07 #6
Spikey
5
I have found the solution. Sorry for every one I just pissed off.
I only had to use the c_str() methode of a string and I'am sure I did try it yesterday multiple times, but I gues I forgot the "()".

greetz

Spikey
Oct 3 '07 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: Tom Warren | last post by:
I found a c program called similcmp on the net and converted it to vba if anybody wants it. I'll post the technical research on it if there is any call for it. It looks like it could be a useful...
16
by: Don Starr | last post by:
When applied to a string literal, is the sizeof operator supposed to return the size of the string (including nul), or the size of a pointer? For example, assuming a char is 1 byte and a char *...
7
by: herrcho | last post by:
i'm in the course of learning C, and found these two words "string, string literal" confusing me.. I'd like to know the difference between them.. Thank you
14
by: Charles L | last post by:
I don't know if this is a stupid quesiton or not. I would like to know how to convert an array of characters generated from a previous operation to a string ie how do I append a null character at...
14
by: Shhnwz.a | last post by:
Hi, I am in confusion regarding jargons. When it is technically correct to say.. String or Character Array.in c. just give me your perspectives in this issue. Thanx in Advance.
6
by: Paulers | last post by:
Hello, I have a string that I am trying to add each char to a datatable row. for example if I have a string that looks like "abcdefg", I would like to break it up into an array of characters...
9
by: somenath | last post by:
Hi All, I was going through one piece of code which is written by an experience programmer and it is working fine. But I think the use of "strstr" is not proper because it may show undefined...
10
by: Lonifasiko | last post by:
Hi, Just want to replace character at index 1 of a string with another character. Just want to replace character at that position. I thought Replace method would be overloaded with an index...
19
by: est | last post by:
From python manual str( ) Return a string containing a nicely printable representation of an object. For strings, this returns the string itself. The difference with repr(object) is that...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.