C++ :: Concatenate Two Char Arrays Into Single Char Array?

Sep 29, 2014

I am trying to concatenate two words from a file together. ex: "joe" "bob" into "joe bob". I have provided my function(s) below. I am somehow obtaining the terminal readout below. I have initialized my memory (I have to use dynamic, dont suggest fixing that). I have set up my char arrays (I HAVE TO USE CHAR ARRAYS (c-style string) DONT SUGGEST STRINGS) I know this is a weird way to do this, but it is academic. I am currently stuck. My file will read in to my tempfName and templName and will concatenate correctly into my tempName, but I am unable to correctly get into my (*playerPtr).name.

/* this is my terminal readout
joe bob
<- nothing is put into (*playerPtr).name, why not?
joe bob joe bob
seg fault*/
/****************************************************************/
//This is here to show my struct/playerInit

[Code]....

View 2 Replies


ADVERTISEMENT

C++ :: How To Concatenate Two Vector Char

Dec 1, 2013

This seamed as a simple thing but i am getting something i did not expect: Example:

Code:
vector<char> StrJoin(SubjSeq.size()+ QuerySeq.size());
cout << StrJoin.size()<<"
"; // size x

StrJoin.insert( StrJoin.begin(), QuerySeq.begin(), QuerySeq.end() );
StrJoin.insert( StrJoin.begin(), SubjSeq.begin(), SubjSeq.end() );

cout << StrJoin.size()<<"
"; // x*2

All structures are vector<char>. when i do the above my characters form Query and Subject are copied in my new vector called StrJoin but the size of that vector is twice the size then it should be.

View 3 Replies View Related

C++ :: Reading Two Words Into Single Char Array From Text File

Sep 15, 2014

I am trying to read in player names (ex: first last) from a text file into the people[].name data struct. I can successfully read in my card file, but I cannot get this to work. I get a seg fault. I believe this is because nothing is actually being read in for my while loops. I can't use std::strings so these must be c-style strings aka char arrays.

// deck of cards
// below are initializations
#include <iostream>
#include <fstream>
#include <ctime>
#include <stdlib.h>
#include <string>

using namespace std;
//globals
const int maxCards = 52;

[Code] .....

View 1 Replies View Related

C++ :: Compare Element Of Char Array And String With Single Character

Dec 3, 2014

how can i compare an element of the char array and string with single chsracter also how to compare char array to cpp string

View 3 Replies View Related

C :: How To Correctly Use Realloc On Array Of Char Arrays

Mar 21, 2013

how to correctly use realloc on an array of char arrays? Say I want to store strings in arrays (=array of char arrays) and double the size of max. strings (y value):

Code:

int x=200;
int y=10;
char *carray[y];
for (int j = 0; j < y; ++j)
carray [j] = malloc (sizeof(char)*x);}
}

[code]...

fix the realloc part of my code?

View 2 Replies View Related

C :: Copy Every Single Character From One Char Into Another

Jun 5, 2013

I have some problems with copying a single character from one char into another. How do I do that? Here is the function I am using

Code:

void
text_col (char line[]) {
char line1[1];
int l, ii;
l = strlen(line);

[Code] ....

I am trying to copy in a loop every single character from char line into char line1. That is not working.

View 8 Replies View Related

C++ :: Comparing Char Array To Char Always Returns True

Dec 23, 2014

I've made a code to check whether or not a save file has been created correctly, but for some reason it always returns this line: readdata[qa]=='1' as true. in which qa is the counter I use in a for loop and readdata is a character array consisting of 50 characters that are either 0, 1 or 2.

this is the entire code:

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

[Code]....

at first is also went wrong at line 22 and also returned that as true, but then I added brackets and it worked.

View 4 Replies View Related

C :: Char Array With A Phrase To Char Word

Nov 28, 2013

I need to do a function that copy every word from a text to a char word. How can i do it?

View 5 Replies View Related

C :: How To Save Char Arrays To Disk

Dec 29, 2013

lets say I have a char array with four elements but only one char is used, does it write four elements or just one?

View 3 Replies View Related

C++ :: Comparing Two Char Arrays Alphabetically

Sep 14, 2013

I want to compare alphabetically two arrays. I try this:

char a[10] = "AABC";
char b[10] = "ZABC";
if(a > b) cout << a;
else cout << b;

But it always outputs a. How can I compare char arrays?

View 3 Replies View Related

C++ :: Template Specialization For Char Arrays

Jun 8, 2012

I'm trying to get template specializations working for char * as well as for char[]. I.e. in the following code the last test does not use the specialization and fails:

Code:
#include <string>
#include <iostream>
#include <cstring>
template<typename T1, typename T2>
bool compare(const T1& lhs, const T2& rhs) {

[Code] ....

View 4 Replies View Related

C++ :: Read Text File Char By Char By Using Vector Class

Oct 29, 2014

Code:

cout<<"Enter Filename for input e.g(inp1.txt .... inp10.txt):"<<flush;
cin>>filename;
ifstream inpfile;
inpfile.open(filename,ios::in);
if(inpfile.is_open())

[Code] .....

View 8 Replies View Related

C :: How To Print The Max Number Row In Relation To The Char Arrays Row

Mar 30, 2013

I have most of the code working properly, but I'm having trouble with a certain area. Currently I have multiple 2D arrays. One is a char array and the other is an int array. In the int array I have to find the max number in each column, which I've done. The problem is, I need to print the max number's row in relation to the char array's row.

For example,

Code: int array[2][3] = {60 50 30 0 100 1}

The max numbers are 60, 100, 30.

char array[2][length+1] = {nameOne nameTwo}

How it needs to print:

nameOne has max score of 60.
nameTwo has max score of 100.
nameOne has max score of 30.

I just can't understand how to compare the two arrays in the right way, so it'll know that nameOne is associated with the numbers in row 0 and nameTwo in row 1, etc.

View 1 Replies View Related

C++ :: Char Arrays - Getting User Input Through Different Methods

May 29, 2014

I've been experimenting with char arrays and getting user input through different methods.

int main() {
char userInput[21];
/*I understand that over here, a maximum of 20 letters can be input, and only letters before a space will be stored in userInput*/
std::cin >> userInput;
std::cout << userInput << std::endl;

[Code] ....

As I was testing, whenever I would input a single word for userInput (for example "hi"), the program would work as expected: it would output "hi" and I'd be able to input a sentence of sorts for userInput2 (for example "hello world") and have it outputted.

But if I were to input more than one word for user Input (for example "hi how are you"), the program would output "hi" as expected, but it wouldn't let me input anything for userInput2 and would just output the rest of the first input; in this case, "how are you" would be outputted and the program would end. I am not aware of the logic error at play.

View 7 Replies View Related

C++ :: Send Two Or More Char Arrays Over TCP Socket (winsock)

Nov 21, 2013

send(sConnect, (userinput, key), 256, 0);

sConnect is already predefined as a socket

Key is a character array of [32]

Userinput is a character array of [256]

How can you send both of them at the same time using the send function without having to create a separate connection?

View 1 Replies View Related

C++ :: Passing Char Arrays Between Functions Of Same Class

Nov 16, 2013

I'm having trouble with passing a character array between functions of the same class. I have a function, buildGraph, that calls function getNextLine. The getNextLine essentially just retrieves the next line of an input file and stores it into a "char line[80]". However when I try to use "line" in my buildGraph function, it has nothing in it.

Here's my code:

Class
#define NUMNODES 10
using namespace std;
#pragma once
class Prog3Graph

[Code] ....

View 4 Replies View Related

C++ :: Concatenate Two 2-dimensional Int Arrays Into One Larger 3-dimensional Array

Jul 31, 2013

How can I concatenate two 2-dimensional int arrays into one larger 3-dimensional array. This question is also valid for the 3-dimensional vectors. I know the command for the one dimensional vector as:

std::vector<int> results;
results.reserve(arr1.size() + arr2.size());
results.insert(results.end(), arr1.begin(), arr1.end());
results.insert(results.end(), arr2.begin(), arr2.end());

and for the one dimensional array as:

int * result = new int[size1 + size2];
copy(arr1, arr1 + size1, result);
copy(arr2, arr2 + size2, result + size1);

But I do not know how to make a 3-dimensional array or vector.

View 3 Replies View Related

C :: Eliminating Use Of Malloc / Free When Copying Char Arrays

Apr 6, 2014

This program works as expected:

Code:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct {
unsigned long long int address;
float current;
unsigned char pressure_units;
}

[code]....

But I don't like how I had to use malloc and free. Is there a different way to accomplish copying the string into a char pointer without resorting to dynamic memory allocation?

View 1 Replies View Related

C++ :: How To Convert Char To Const Char

Jun 3, 2013

I have a file which contains a year and the name of an associated file to be read. I need to extract the data in the txt file and perform some calculations.

( year data file)
2004 2004data.txt
2005 2005data.txt
2006 2006data.txt

Here is what I do. I first declare "char yeardata" and then pass "2004data.txt" to it. Then I call yeardata in ifstream to extract the data inside the file "2004data.txt". The problem is that char yeardata is not constant so I cannot pass the file to it. It doesn't work if I change "char yeardata" to "const char yeardata".

Code:
int oldnewcomp_temp(char* lcfile) {
using namespace std;

int year;
char yeardata;

[Code] ....

View 12 Replies View Related

C++ :: Get Word From Char Array

Jan 28, 2015

Let's say i have some text in char array Code: char text[] = "Hello my friend"; How can i get a seperate words from it? It should be like

Code:
char a[] = "Hello";
char b[] = "my";
char c[] = "friend";

Is it possible to do?

View 2 Replies View Related

C :: Int Array To Char (Binary Value)

Jul 19, 2013

I have an int array with the binary value of a char. How to to turn it into a char? I can transform it to an int but then I'm stuck.

Code:
#include <stdio.h>
int main() {
int a[260] = {0,1,1,0,0,0,0,1};
char buf[260];
int b=0;
for(int i=0; i<8; i++) {
b=10*b+a[i];
}
itoa(b, buf, 10);

View 6 Replies View Related

C :: Unable To Set Char Array

May 5, 2013

Code:

#include <stdio.h>
char * strcpy(char *restrict s1, const char *restrict s2);
struct item {
char title[20];
struct item *next;
} firstCard;
int main (){
strcpy(firstCard.next->title, "whatever");
}

I am unable to set firstCard.next->title

View 2 Replies View Related

C++ :: Char Array To String

Oct 19, 2013

I have some code:

char cHomeTeamFaceOffsPercentageWon[100];
memcpy(cHomeTeamFaceOffsPercentageWon,cSqlHomeTeamFaceOffsPercentageWon,100);

After this, for example, cHomeTeamFaceOffsPercentageWon is, "29%".

Then, I use

std::string szwPercentageWon = std::string(cHomeTeamFaceOffsPercentageWon);

szwPercentageWon is then, "2". Shouldn't this convert correctly, to "29%" as well.

Or is there something I'm missing? Maybe the termination character, or something.

View 1 Replies View Related

C++ :: Using A Pointer To Char Array

Aug 31, 2013

I like to use a Pointer to char array. And then I would like to do a Pointer Arithmetic by incrementing the Pointer. Finally I would like to see the Addresses of the Pointer to each of the char Array Elements. I had created a program below, but I am not getting any Addresses from my Pointer.

#include <iostream>
using namespace std;
int main () {
int ArraySize;
char ch[]= "This is a Char Pointer";
char* iPtr = ch;

[Code] ....

View 3 Replies View Related

C++ :: Array Of Char - Shows More Than It Should

Nov 9, 2013

I have this code:

#include <iostream>
#include <string.h>
#include <fstream>
using namespace std;
ifstream f("input.txt");
ofstream g("output.txt");

[Code] .....

This program make a copy of the source array, then it removes each character and shows the obtained array.

And when i insert "abcdefghijkl"

it should show this: "

#include <iostream>
#include <string.h>
#include <fstream>
using namespace std;
ifstream f("input.txt");
ofstream g("output.txt");

[Code] .....

View 1 Replies View Related

C/C++ :: Comparing 2D Char Array?

Jun 22, 2014

I am working on a program to find uppercase, lowercase and digits in a 2D char array. When I try to use an if statement to increase the counter, I get an error "no conversion from 'int' to 'char*'". This is the if statement I am using.

if(myArray[j] <='9' || myArray[j] >='0')

View 7 Replies View Related







Copyrights 2005-15 www.BigResource.com, All rights reserved