C++ :: How Can Numeric Values Stored As Strings Be Converted To Numbers

May 16, 2013

I need to check my understanding from some questions about strings. How can numeric values stored as Strings be converted to numbers?

a)vector of required numeric data type
b)atoi function in cstdlib library
c)cout statement with required numeric data type

I picked b), I am aware of atoi, atol, and atof as methods to convert, but are there other methods?

What is the purpose of strncat function?

Combines n characters from source string into target string

C++ string provides:

a)convenient way to declare and manage character arrays
b)functions to manipulate strings
c)all of the above
d)none of the above

I picked c)

View 2 Replies


ADVERTISEMENT

C++ :: Unsigned Char Array - Assigning Values Converted From Double

Aug 3, 2014

I'm having a pretty weird problem. I've created an unsigned char array for an image buffer:

buffer_rgb = new unsigned char[_w * _h * 3];
memset(buffer_rgb, 0x0, sizeof(unsigned char)* _w * _h * 3);

And I add pixel color values to it like so:

buffer_rgb[i] = ((unsigned char)(col[0] * 255));
buffer_rgb[i + 1] = ((unsigned char)(col[1] * 255));
buffer_rgb[i + 2] = ((unsigned char)(col[2] * 255));

Where col is a 'vec4' struct with a double[4] with values between 0 and 1 (this is checked and clamped elsewhere, and the output is safely within bounds). This is basically used to store rgb and intensity values.

Now, when I add a constant integer as a pixel value, i.e.:

buffer_rgb[i] = ((unsigned char)255;

Everything works as it should. However, when I use the above code, where col is different for every sample sent to the buffer, the resulting image becomes skewed in a weird way, as if the buffer writing is becoming offset as it goes.

These two images illustrate the problem:

tomsvilans.com/temp/140803_render_skew.png
tomsvilans.com/temp/140803_render_noskew.png

You can see in the 'noskew' image all pixels are the same value, from just using an unchanging int to set them. It seems to work with any value between 0-255 but fails only when this value is pulled from my changing col array.

Whole function is here:

// adds sample to pixel. coordinates must be between (-1,1)
void Frame::addSample(vec4 col, double contrib, double x, double y) {
if (x < -1 || x >= 1 || y < -_aaspect || y >= _aaspect) {

[Code] .....

View 1 Replies View Related

C :: How To Compare A String With A Set Of Strings That Have Been Stored In A File

May 5, 2014

I am trying to compare a string that i have entered with a set of strings that have already been stored in a file. I am using strcmp function but i am not getting the result.

Code:
printf("
Enter string:");
scanf("%s",&m);
ptr_file =fopen("abc.text","r");

[Code] .....

View 10 Replies View Related

C/C++ :: Compare Values Stored In First Array To User Inputted Values In Second Array

Oct 19, 2014

Goal: Write a program that compares the values stored in the first array to the user inputted values in the second array.

In order to fix this error: [URL]...

I had to change my array initialization to one with a star in front of it:

char a1[]={"a","d","b","b","c","b","a","b","c","d","a","c","d","b","d","c","c","a","d","b"};
to:
char *a1[]={"a","d","b","b","c","b","a","b","c","d","a","c","d","b","d","c","c","a","d","b"};

I also changed my 2nd array to one with a star in front of it: char *a2[20];

What does this mean exactly? Putting a star in front of an array?

Also, I am now getting an "unhandled exception" when I try to get input for my 2nd array:

cin>>a2[i];

View 3 Replies View Related

C :: Accessing Values Stored By A Pointer Into Array

Mar 7, 2013

I am writing a C program to access a string into an array from a pointer array in Visual Studio 2010. Program is given below:-

Code:

#include <stdio.h>
#include <string.h>
void main() {
char data_a[6],data_b[6]="ABcde",*ptr;
ptr=&data_b[0];
data_a[0]=*ptr;
printf("%s",a);
}

I am getting some junk character first and then my actual data on console window. I want to where I am getting wrong and how to resolve it.

View 4 Replies View Related

C :: Retrieving Values That Are Stored To Compact Flash Card

Sep 19, 2013

My specific request is for retrieving values that are stored to a CompactFlash card. I am able to store values (we call them recipes) to the CompactFlash card as .csv file. I just haven't been able to figure out the code to retrieve this information back to the touchscreen.

Code:
// Create a new folder if it doesn't exist
CreateDirectory("/recipes");
//Create the file if it doesn't exist
CreateFile("/recipes/recipe.csv");
//open the file
hfile = OpenFile("/recipes/recipe.csv", 2);

[Code]....

Now with that said, I would like to retrieve these values from the .csv file.

View 7 Replies View Related

C :: Load Up File That Contains Series Of Int Values Stored In ASCII

Sep 23, 2013

This seems like a fairly straight forward assignment. Load up a file that contains a series of int values stored in ASCII and print out the ASCII characters to the console.

The problem I am having is that I am getting the numerical value of bytes ("40" for 10 numerical values, "200" for 50 values). The numbers are generated randomly by another file, but I can control how many numbers are generated. For example, if I type in:

shell% cat tempfile

the output is as follows:

/8?qE?. Y4?(T???a???%@

but when I try to run it with my program:

shell% ./intcat tempfile

the output displayed is:

40

Code:

#include <stdio.h>
#include <assert.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <stdlib.h>

int main(int argc, char *argv[]){
//check if arguments match

[Code] .....

View 10 Replies View Related

C++ :: Using Isalpha With String Converted To Char Array

Jan 26, 2014

I am new to C++ and I have a two player word guessing game working well. However, I would like to be able to validate whether the word entered by player 1 is a completely alphabetic word using isalpha.

The error I am getting right now is as follows:

"error: array must be initialized with a brace-enclosed initializer
char str[100]=hiddenwordtwo;"

/* isalpha portion of code */
#include <stdio.h>
#include <ctype.h>
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <string>
#include <cstring>
using namespace std;

int main () {
char hiddenwordtwo[100];

[Code] .....

View 2 Replies View Related

Visual C++ :: Using Wstring Converted From Char In DLL Class

Jun 17, 2013

I figured it out when I built a simple demo project. Problem arose because of trying to access a c-wrapper dll from the app class whereas the wrapper class had not been initialized there but rather in the main dialog class - so naturally it didn't work!!! Anyway, I've attached the demo for any who might be interested, but I regard the problem as resolved. Shows the value of building simple projects to isolate a problem. I failed to organize the order in which such a program initializes - I guess it's always App first, then MainFrame, then Doc and View (I think).

View 5 Replies View Related

C/C++ :: Parsing Integers Values From Strings

Sep 30, 2014

So I have this assignment to read a file in, malloc some arrays, run it through a perceptron and to display the final weights. I have the majority of it already written but this is only my third program in C and I'm more familiar with Java and Python than C.

The problem I'm having is when I read in command line arguments, I can't seem to parse integer values from the strings in argv[i] by using atoi().

I've included the piece of code where I'm trying to 'parse.' I understand atoi convers ascii to integer, but I don't understand if it just gives you the ascii code or the number that it actually represents. I attempt to use atoi on lines 33-35

The input arguments in the command line are:

bob in.csv 100 5 10

int main(int args, char* argv[]){
int ** ra; // array of array of pointers
FILE *ifp; // file pointer
char cc; // char var for reading input from file
int i = 0; // counter

[Code] ....

View 3 Replies View Related

C :: Convert Strings From Hex Values To Decimal Equivalents

Feb 8, 2014

I understand most of program below. Essentially, we have strings that we want to convert from hex values to decimal equivalents. We check if first two characters of string are 0x or 0X, which signifies hex format. If our hex string consists of solely digits like 0x25, then the processing is simple. We take the digit assign it to answer variable, and for each additional position in the hex base-16 system, we multiply the digit by 16.

Now if the hex string is something like 0x2A, then for 'A', the hexalpha_to_int() function is called, since we are able to find 'A' in the hexalpha string, we take the value of 'A', which is ascii 65 divide it by 2 and add 10 to it: 65/2+10=42.5. This doesn't make sense. What is the purpose of this logic right here: 10 + (i / 2).

Code:
#include <stdio.h>
#include <stdlib.h>

int hexalpha_to_int(int c){
char hexalpha[] = "aAbBcCdDeEfF";

[Code] ....

View 1 Replies View Related

C++ :: Strings - Read Multiple Values In On A Single Line

Jul 28, 2014

My question is on c++ strings. At the moment my program is reading input in one line at a time after the user presses enter.

I want to read multiple values in on a single line. Example: "apple banana orange end" ... How would I do this?

MAIN Code:
#include "Header.h"
#include "Orange.h"
#include <iostream>
#include <string>
#include <cctype>
using namespace std;

[Code] ......

View 11 Replies View Related

C++ :: Read All Values / Strings Form Unknown File

Nov 4, 2014

For an assignment I have to write a program which basically converts 8 bit binary numbers to ASCII and outputs the assembled text. Here's the catch:

The 8-bit binary numbers are provided by some external file (which only contains 8 bit binary numbers); the name and hence length is not known. The external file is called with a pointer upon execution

(./"conversion program" < external_file.in).

I'm getting the 8 bits as a string, calculate/convert decimals, output char type. HOW do I know when to stop the loop? If I just pick an insanely high number I get random stuff at the end; no boundaries obviousely lead to an infinite loop. Can I determine the lenght of this random ext file somehow nonetheless?

Is it possible to create a vector which dynamically adjusts itself until there are no more strings = end of the file?

View 6 Replies View Related

C++ :: Add Two Numbers As Strings

May 19, 2013

I need to make a small program with a function with this prototype: void f(char *a,char *b) that adds two numbers represented as strings without using conversion operators or other tricks.

View 17 Replies View Related

C++ :: Compare Two Strings Of Numbers

Jan 4, 2014

I am trying to figure out how to go about comparing two strings of numbers. I have two files that both contain numbers 1-50, one file has multiple repeating numbers while the other one just has 1-50.

I want to compare the files and count how many of each number a occurred and make a chart with * next to the number. First I figured I would use the strings like an array and compare them using nested loops. Then I noticed I have single and double digit numbers. The numbers in the files are printed as:

1 44 5 34 4
2 22 7 55 4
...... etc

Compared too:
1
2
3
4
5
......
50

I thought about using string stream and converting the string to int but wouldn't it just be a huge number when set to the int variable? Then I thought about a array initialized with 1-50 and compared to the file but I still have the issue with single and double digit numbers.

My question is how can I just read one number at a time, either double or single digit?

View 11 Replies View Related

C :: Adding Two Numbers Taken As Strings

Dec 13, 2014

I was trying to solve a problem that required to add one hundred 50 digit numbers. Since there is no way to hold such a huge number. I read that storing them in strings is the way to go. I was caught midway while trying to do the same.

Here's the source code;

Code:

#include<stdio.h>
#include<string.h>
const char * addTwoStrings(char *number1,char *number2)
}

[code]...

And the text file is this. Code: 123465789 321654987 This isn't the exact huge number, but I wanted to try it out with lower number before trying out with the original huge ones.I am trying to store the numbers in a two-dimensional array. However when I and try to pass the single number as an parameter to the AddTwoStrings() method, It actually passes the entire number as such.

When I pass string[0],string[1] it should pass the first and second number from the files as the two numbers instead of the whole number as such.The function AddTwoStrings() doesn't do anything as of now, I encountered this error when I was testing the code till this part.

View 2 Replies View Related

C/C++ :: Madd Libs Game - How To Store Inputted Strings Or Values To Arrays

Nov 11, 2014

My assignment is writing Madd Libs game. I still do not understand how to store inputted strings or values to arrays. I need explanation of collecting inputted data to arrays.

#include "stdafx.h"
#include<stdio.h>
//#include<string.h>
int main(void) {
char string[37] = {''};
char adjective1[15];

[Code] ....

View 14 Replies View Related

C++ :: Adding Two Numbers Represented As Strings?

May 19, 2013

I need to make a small program with a function with this prototype: void f(char *a,char *b) that adds two numbers represented as strings without using conversion operators or other tricks.

View 8 Replies View Related

C :: How To Enable Numeric Variables

May 22, 2013

working on this code I have encountered a few issues. My program lists the occurrence of each letter but i'm unsure of how to enable numeric variables. how to improve the overall quality of the code.

Code:

#include <stdio.h>
#include <string.h>
int main()
{
char string[100], ch;
int c = 0, count[26] = {0};
printf("Enter a string

[Code]...

View 4 Replies View Related

C :: How To Check Input For Non Numeric Value

Mar 20, 2013

Description: This program asks the user to enter a Fahrenheit temperature and then converts it into Celsius and Kelvin temperature.

Code :

#include <stdio.h>
void Temperatures(double temp_F);
int main(void) {
double temp;

[Code]...

View 6 Replies View Related

C++ :: How To Convert Char Into Numeric Value

Jan 1, 2015

when I was looking for a way how to convert char into numeric value for std::cout I found some old discussion with this statement: Operations on unsigned int are typically faster than on unsigned char, because your processor cannot fetch single bytes but has to get at least a multiple of 4 and mask out the other 3 bytes. Is this true? By using smaller (in bytes) variable I actually slow down my program? I always thought that it is a good practice if I use the smallest variable which will do the work. Is it also dependent on a compiler and OS?

View 2 Replies View Related

C# :: Possible To Convert Char Into Numeric Value

Feb 22, 2014

I want to avoid converting the char[] into a string as an intermediate step, since I'm trying to write some "string" parser helpers which don't allocate a bunch of different strings onto the heap. (whole point of this little project is to reduce GC pressure in applications that do alot of string parsing).

basically if I have a char[] that contains {'1','2','3'}, I'd want to to be converted into 123.

I tried messing around with the stackalloc operator in C#, but its illegal to stackalloc a string unfortunately. I also googled around for converting a char[] into a numeric value, but all the solutions convert each individual char into their ASCII code, or convert the char[] into a string as an intermediate step.

View 12 Replies View Related

C++ :: Getting Perfect Numbers From Two Values

Oct 22, 2013

Find the perfect numbers between a starting value and a finishing value entered by the users. Display the results to the screen.

I've been told that I need to modify things under the void and the calcdiv in my main () but I can't seem to figure it out. It needs display the perfect numbers between the starting and finishing value. e.g. 28 and 429.

#include <cstdio>
#include <iostream>
#include <cstdlib>
#include <cmath>
using namespace std;
void calcdiv(int number) {
for (int i=1;i<=number/2;i++)

[Code] .....

View 3 Replies View Related

C# :: Creating Object That Uses Numeric Up / Down And Track Bar?

Nov 19, 2013

I want to create an object that uses visual C#'s Numeric Up/Down and a Track bar.

The reason is that I have a GUI that uses lots of these linked together with events and I want a common event handler for all of them.

Maybe just adding a property that is a Numeric Up/Down in the Track bar and vice-versa will allow me to create a common event for all of these pairs.

View 1 Replies View Related

C++ :: Numeric Conversion - How To Convert Int Into String

Jun 3, 2013

How to convert int into string ? I had done conversion string to int.

My code :

/*Convert string into int using stringstream */
#include <iostream>
#include <string>
#include "console.h"
#include <sstream>
using namespace std;

[Code] .....

View 3 Replies View Related

C++ ::  Expected Identifier With Numeric Limits

Aug 22, 2014

I'm trying to perform a simple input operation, and check to make sure that whatever the user input is a valid input (is within data type bounds, is correct data type...). But when I get to ignoring the input, I keep getting an error.

unsigned short num;
while (true) {
std::cin >> num;
if (std::cin.fail()) {
num = 1;

[Code] ....

I don't fully understand why this error is here.

View 6 Replies View Related







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