C :: Best Way To Store Input String Into Char
Sep 16, 2013
I'm extremely rusty at C but is this the best way to store an input string into a char*?
Code:
int length = 100; //initial size Code: char * name = malloc(length * sizeof(char)); //allocate mem for 100 chars
int count = 0; //to keep track of how many chars have been used
char c; // to store the current char
while((c = getchar()) != '
'){ //keep reading until a newline
if(count >= length)
name = realloc(name, (length += 10) * sizeof(char)); //add room for 10 more chars
name[count++] = c
}
Is this a good way and what could be better?
View 2 Replies
ADVERTISEMENT
Apr 25, 2014
I am trying to store each char of a string(string a ="1100") into a byteArray ( byte[] byteArray = new byte[4]. its not showing any error but its storing like below:
byteArray[0] = 49
byteArray[1] = 49
byteArray[2] = 48
byteArray[3] = 48
and what i want is
byteArray[0] = 1
byteArray[1] = 1
byteArray[2] = 0
byteArray[3] = 0
I don't know why but its replacing 1 with 49 and 0 with 48.what am I doing wrong or how to do this?
my code is as below
byte[] byteArray = new byte[4];)/>
int binArrayAdd = 0;
string a ="1100";
foreach (char Character in a)
{
byteArray [binArrayAdd] = Convert.ToByte(Character);
binArrayAdd++;
}
View 4 Replies
View Related
Nov 22, 2013
I'd like to input a file and store the contents of the file in a string.
Here is my code:
std::string inputFile();
int main() {
std::string fileContents = inputFile();
} std::string inputFile()
[Code] ....
It works fine if the file name and path is input correctly.
But, if the file name or path is entered incorrectly, the recursive call to inputFile is executed, and the user is given another opportunity to enter the file. Then, if the file name is entered correctly an error is thrown in Visual Studio 2013:
"Unhandled exception at 0x77F7A9E8 (msvcr120d.dll) in Assignment4.exe: 0xC0000005: Access violation reading location 0xCCCCCCC0."
How could this be done better?
View 1 Replies
View Related
Feb 8, 2015
I am writing a program that asks the user for their gender. I know that atoi converts arrays into integers, but I need the input from the user in the form of F or M into a char, and return it to the main function to be displayed at the end of the program.
string Employee::getgender(char gen)
{
cout << "Please enter your Gender: " << endl;
//atoi function would go here, what for char?
getline(cin,gen)
return gen;
}
View 1 Replies
View Related
Sep 24, 2014
I want to take string input into a char array.
What is the functionality for the above problem.
View 1 Replies
View Related
Apr 22, 2014
I am trying to store the contents of a text file into a char array. However the function i am using ifstream member function get(); seems to stop working when fed with certain characters. Is there another solution besides the get() function that will accept all types of characters from files?
char text[1000];
for (int i = 0; i <= textlen; ++i)
{
text[i] = text_in.get();
}
View 3 Replies
View Related
Oct 10, 2014
I want to store the address of a customer (with spaces) in a char variable (say cadd). First I tried to use "cin", as we know it reads until it sees any whitespace. So it reads only first word before a white space. So, I used "getline()" function, it will work. But when I used it, It did'nt wait for the I/P (it skipped it).
char cadd[20];
std::cout<<"Enter Customer Address:
";
std::cin>>cadd;
std::cout<<"Enter Customer Address:
";
std::cin.getline(cadd,20);
View 3 Replies
View Related
Oct 30, 2013
"Every number has at most 500 digits" under input. 500? How am I supposed to store that? And what if someone multiplies 999 ...(500 times) * 999 ... (500 times) ? I seriously doubt that my computer can store that! Is that just some huge value used to scare people off or is there some sneaky trick that I am unaware of?
I am done with the program, but won't post it, Lets not ruin the fum for ohers...Does that 500 limit also applies to result of operation?So max length of an input number is ~22 digits ? But still, how do I store 500 digits? Array would be a lot of wastage of memory (though I do have 256MB available).
View 2 Replies
View Related
Nov 8, 2013
This program works but i need to build an additional loop that will re-prompt a user if his initial entry (int y_n) is other than 'Y', 'y', 'N', 'n'. For instance if a user tries to enter 'Yup', he will be prompted "That isn't a valid entry" and then re-asked to enter int y_n.
Note: If user answers Y he is asked to enter a value that is entered into an array. If the user at any point answers N, the program ends and final stats are cout.
#include <iostream>
using namespace std;
int main(){
char y_n;
int i = 0;
float input_value;
float myarray[100];
[Code] .....
View 1 Replies
View Related
May 4, 2013
I can`t seem to store multiple values into my vector when a user keys in an input. Lets say a user inputs 1,2 as the first set and later keys in 3,4.
My vector gets overridden by 3 and 4. I'm trying to pass these values into a function to store them into a vector and use them in the function.
Below is my code snippet
int reconstructSecret(int x, int y) {
int getX,getY;
getX=x;
getY=y;
vector<int> myXCordVec;
vector<int> myYCordVec;
[Code] .....
View 6 Replies
View Related
Feb 20, 2015
I am trying to get user's voice as an audio input to a c++ program and store it in a text file (.txt)
I have been searching over the web to find some kind of library or something like OpenCV(video library) for while now but nothing seems to be there.
View 2 Replies
View Related
Jul 2, 2013
int main() {
char StudentName[4][10] = { "Hermine", "Paul", "Gertrude", "Leon" };
cout << "Student Names";
cout << "
Student 1: " << StudentName[0];
[Code] ....
This code work fine...but i want to take name from user..how i store it in character array through getline()..
View 6 Replies
View Related
Nov 5, 2013
Read the input file of data ( employees.txt) and store them in arrays. This company can have up to 55 employees [b]i need to do these following in these program:
Write a function to read the input file and store the data in arrays.
Write a function to calculate regular pay.
Write a function to calculate overtime pay
Write a function to calculate gross pay.
Write a function to bubble sort the employees into order by last name, first name.
Write any swap functions that are needed.
Write a function to write output to a file called payroll.txt
Format of file is EMPLOYEES.TXT[/b]
Hours Pay Rate Employee Number First Name Last name
40.0 10.00 A1234 Jane Adams
50.0 10.00 L8765 Mary Lincoln
25.5 10.85 W7654 Martha Washington
52.0 15.75 A9876 John Adams
45.0 25.00 W1235 George Washington
40.25 55.00 L9087 Abraham Lincoln
30.0 9.75 T9876 William Tell
42.5 12.50 M7654 Missy Muffett
30.0 10.00 P8765 Peter Piper
HERE IS MY CODE SO FAR:
#include <iostream>
#include <cstdio>
#include <fstream>
#include <string>
#include <iomanip>
using namespace std;
void tellUser();
[code]....
View 9 Replies
View Related
Jan 10, 2015
I'm coding a hangman game. I'm trying to store user entries so i can output them to show the user what they have already entered. Problem is that it's not display anything at all.
I'm trying to store multiple characters of course, and then display all characters stored.
char guess[27]={0};
cin >> guess[26];
int hit=0;
for(int i=0; i<len; i++) {
if( guess[26] == hidden_word[i] ) {
hit++;
select_word[i] = guess[26];
if(strcmp(hidden_word,select_word) == 0) {
[code]....
EDIT: I also get the error - Stack around the variable 'guess' was corrupted. At the end of the game.
View 6 Replies
View Related
Apr 6, 2014
My program is designed to read input from the user and then store that input in a structure variable. Whenever i use the cin.getline() function, it comes up with a semantic issue. Here is the code:
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
const int DESCRIPTION_SIZE = 100, DATE_SIZE = 20;
struct inventory {
string description[DESCRIPTION_SIZE];
[Code] .....
I left out the other functions. The error reads "no matching member function for call to 'getline'.
View 6 Replies
View Related
Mar 10, 2015
I'm trying to read the data from a file i/o and put them into an array of structs. But when I run the program, it gives me a bunch of "garbage" as an output. my code and see
#include<stdio.h>
#include <stdlib.h>
#include<string.h>
[Code].....
View 7 Replies
View Related
Mar 15, 2015
I'm trying to write a function in C that calculates the average test score given by the user an arbitrary number of times...
View 14 Replies
View Related
Feb 19, 2014
Write code to do the following:
1) declare a variable ptr as a pointer to int and initialize it to NULL
2) dynamically allocate memory for an array of 100 elements
3) read 100 elements from the standard input device and store them in the array.
This is what I have so far, I'd like to know if its ok or if something is wrong.
int *ptr = NULL;
ptr = new int[100];
cin >> dataPtr [arr];
View 2 Replies
View Related
Feb 11, 2013
Im trying to write a program that reads in strings and decides if the 1st one is repeated. I cant figure out how to store the first string into a variable, and compare that variable to the rest of the inputted strings.
Code:
#include <strings.h>
#include <stdio.h>
int main () {
//Declared variables
int i;
}
[code]....
View 3 Replies
View Related
Dec 20, 2014
I have the codes for such a problem where, to create a program that counts how many times the second string appears on the first string. Yes it counts if you put 1 letter only, but if you put 2, it is an error. As an example. If the first string is Harry Partear, and the second string is ar, it must count as 3. Here's the code:
Code:
#include <iostream>
#include <conio.h>
using namespace std;
int main ()
[Code] ....
View 6 Replies
View Related
Apr 20, 2013
I'm trying to "tokenize" a string using std::string functions, but I stored the text in a char array. When I try to convert it to a string, the string has the first character right but the rest is garbage.
// Get value from ListBox.
char selectedValue[256];
memset(selectedValue, NULL, 256);
SendMessage(GetDlgItem(hWnd, IDC_LB_CURRENTSCRIPT), LB_GETTEXT, selectedIndex, (LPARAM)selectedValue);
// Convert to string.
string val(selectedValue);
[Code] ....
View 3 Replies
View Related
Feb 5, 2013
A string class that finds a char in the string?
View 1 Replies
View Related
Oct 14, 2014
I am having trouble with parsing out string value into a 2D vector. Suppose i have the string "attack at dawn " consisting of 15 characters, i will like to store it into a 2D vector with 5 rows and 3 columns and the result is as follow.
Vector[0][0] = "a"
Vector[0][1] = "t"
Vector[0][2] = "t"
Vector[1][0] = "a"
Vector[1][1] = "c"
[Code] ....
View 1 Replies
View Related
Oct 7, 2014
Like if the user enters "38 F" how do I take out the 38 only and store it in a variable? So far my program goes like this:
string text;
double temperature;
cout << "Enter temperature: ";
getline(cin, text); // user enters 38 F
temperature = text // store 38 from string into double
View 1 Replies
View Related
Oct 13, 2014
I am having trouble with parsing out string value into a 2D vector. Suppose I have the string "attack at dawn " consisting of 15 characters, i will like to store it into a 2D vector with 5 rows and 3 columns and the result is as follow.
Vector[0][0] = "a"
Vector[0][1] = "t"
Vector[0][2] = "t"
Vector[1][0] = "a"
Vector[1][1] = "c"
Vector[1][2] = "k"
Vector[2][0] = " "
Vector[2][1] = "a"
Vector[2][2] = "t"
etc...
Here is a draft code that i did but is not working as desired.
vector<vector <string > > plaintextVector;
vector<string> row;
string totalString = "attack at dawn ";
int dimension = 3;
[Code] ....
View 4 Replies
View Related
Dec 3, 2014
I have a file this is made by 7 column. I want to separated them as columns and stores as arrays.lines should be 0 ,1 ,2 ,3 .. and arrays are the name 0,1,2,3,.. my program is not opening and giving me mistakes such:,
read_from_file
ead_from_file
eading_from_file.cpp(26): error C2784: 'std::basic_istream<_Elem,_Traits> &std::operator >>(std::basic_istream<_Elem,_Traits> &,std::basic_string<_Elem,_Traits,_Alloc> &)' : could not deduce template argument for 'std::basic_istream<_Elem,_Traits> &' from 'std::basic_ostream<_Elem,_Traits>'
And my code is
#include <iostream>
#include <fstream>
#include <sstream>
#include <vector>
#include <string>
using namespace std;
int main(){
int i;
[Code] ....
View 8 Replies
View Related