C/C++ :: Taking String As Input And Making It As Whole Array (string Literal)
Oct 19, 2014
Very new to programming, and I know that there must be another way on inputting a string into each array cells not by just inputting it one by one, but as a whole. My code at the meantime is: [URL]
View 1 Replies
ADVERTISEMENT
Jun 26, 2014
/*assume array is already initialized and declared and is of array type string.*/
int i = 2;
int j = 1;
string newvalue;
cout<<"Current value at array[i][j] is "<<array[i][j]<<endl;
cout<<"Enter new value "<<endl;
cin>>newvalue;
array[i][j]= newvalue; //PROBLEM IS IN THIS LINE.
cout<<endl;
cout<<array[i][j]<<endl;
I'm having lots of trouble with storing a cin string text into a string array. It just seem that after I cin newvalue, the program crashes. Is this way of storing it considered illegal? I'm just a beginner with 5 months of coding experience in C++.
View 1 Replies
View Related
May 28, 2013
I have a binary identifier which I tried to make a constexpr since all of its calculations would never occur during runtime (this is true for literal identifiers, right?). Since constant expressions can only have one instruction, I tried to cheat a little and returned an immediate call to a lambda function. This failed miserably however. I tried making a constexpr function pointer and called that from _binary down below, but the compiler still felt that it wasn't a constexpr. Why is this? And is there a way to make a function like _b below constexpr?
#include <iostream>
#include <cstddef>
using std::cout;
using std::endl;
long unsigned //Original
operator"" _b(const char* const literal, size_t lsize){
[Code] .....
View 4 Replies
View Related
Sep 13, 2013
In another forum, this example code fragment was stated as being an example of undefined behavior. My understanding is that a literal string exists from program start to program termination, so I don't see the issue, even though the literal string is probably in a different part of memory.
Code: /* ... */
const char *pstr = "example";
/* or even */
char *pstr = "example";
/* as long as no attempt is made to modify the data pointed to by pstr, */
/* unless pstr is later changed to point to a stack or heap based string */
View 11 Replies
View Related
Jan 8, 2014
This compiles o.k.:
Code:
int main(void){
char *a;
a = (char*) malloc (100*sizeof(char));
[Code]....
I get an error saying "pointer being freed was not allocated". This happens for free(a), free(*a), free(&a), free(&*a).
So if I no longer need "1234567"... how do I get rid of this memory element?
View 7 Replies
View Related
Apr 19, 2014
In the following char array, notice the use of a backspace character in a string literal: ''.
char text1[50] = "aHello,
World! Mistakee was "Extra 'e'"!
";
What exactly does a backspace character do here? When the compiler evaluates this line, does it actually delete the previous character, like when you press the backspace button on the keyboard?
View 1 Replies
View Related
Oct 6, 2014
how string literal that works with the cin object?
char * str = "This is a string constant";
Is the str stored the address of the first character of the string literal?
But some books just state that the pointer-to-char (char pointer) stores the address of the string literal". So just wonder how it is.
When it is used with cout, cout just treats it like a string and instead of printing the address, it just prints out all characters one by one until it reaches the terminated null character.
If this is the case, then I am just wondering how cin works with it? with a statement like this cin >> str; ?
Does the computer allocate enough memory for it? and then cin stores the first character into the first address and then advances to the next address and stores the next character?
View 1 Replies
View Related
Feb 23, 2015
I have a struct like this:
Code:
struct String{
char* data;
std::size_t size;
};
I would like to be able to create const variables of this type for string literals.
Code:
const String message;
Is there an elegant way to create a const String like this when data is a string literal?
I tried this:
Code:
const char *string_data = "Hello";
size_t string_size = strlen(string_data) + 1;
const String string = {string_data, string_size};
The problem with that is that string.data isn't considered const during the initialization of the String struct so the compiler throws an error. It doesn't feel very elegant to do it like this either way.
Is there an elegant solution to this problem? I would like to avoid making a copy of the string literal.
View 7 Replies
View Related
Jul 10, 2014
I'm making a .json loader for a project that I'm working on, and to simplify things, I decided to make all the root attributes named in a separate file. Here's my problem: my loading function, Add(const char* id), works just fine when I pass it a string literal.
However, when I use a function that iterates through a vector list, even with the exact same definitions as the literal, it returns the error: std::out_of_range at memory location 0x0026fb30
I've stepped through it with the VS2010 debugger about a hundred times, checked against memory locations, and just have done everything I can think of, all to no avail..
The code, with data-checking omitted:
The std::map I'm adding to:
static std::map<const char*, Item*> *s_mItems;
Initialized as std::map<const char*, Item*> *Item::s_mItems;
Add() Function (Works by itself with a literal):
static bool Add(const char* id) {
...
std::string name = node.Get("name").ToString();
std::string desc = node.Get("description").ToString();
int rarity = StrToRarity(node.Get("rarity").ToString());
[Code] ....
AddList() function, where the program always breaks:
static void AddList(std::vector<std::string> list) {
for(std::vector<std::string>::iterator it = list.begin(); it != list.end(); it++) {
Add(it->c_str());
}
}
View 3 Replies
View Related
Jan 24, 2014
Background: I'm using SDL and CodeBlocks and trying to make a Graphics class that would simplify some SDL operations such as drawing and loading images.
Issue: The loadImage function in the graphics class fails to load the image correctly and so the program prints out a blank window during run-time. I've tried multiple ways of passing a string literal into the function the surface temp fails to load and so background in Game fails to load. After testing it several times, I'm pretty sure that the issue lies with SDL_LoadBMP not registering the passed variable for whatever reason. I know the image is in the right place as writing SDL_LoadBMP("./Graphics/image.bmp"); brings it up just fine.
Current Code:
Main simply creates a Game object and execute(), so I didn't feel the need to put it on here.
Game.h
#ifndef GAME_H
#define GAME_H
#include "SDL/SDL.h"
#include "SDL/SDL_ttf.h"
#include "Graphics.h"
#include <string>
#include <cstring>
using namespace std;
//GLOBAL CONSTANTS
//game window settings
[Code] ....
Output: A blank window. (It should show the background image but doesn't.)
Note: I originally wrote it as gfx.loadImage(background, "./Graphics/image.bmp") but that gives me a conversion warning and still fails to show the image when the program runs. I've tried looking up examples similar to what I was doing but no one else seems to have this problem.
View 3 Replies
View Related
May 9, 2014
Debug Error
ProjectsFinal ProjectGrItemDebugGrItem.exe
R6010 - abort() has been called
I was going over this with a friend and it seems as though getline() is not reading anything in and thus throwing the abort error. I'm not sure why this is because I've included the textfile, with the correct name of course, in both the regular file location and the debug folder. I ask for user input and the user then inputs the name of the file they want, I do some required things behind the scenes and display the results for them in a cmd window. I've included pastebin files for both my header and cpp files because it is far to large for one post I shall, however, post the full code in the comments.
Quick Code
The problem occurs on line 159. I'm assuming once this line is fixed, line 163 will have the same problem.
// Read regular price
getline(nameFile, input, '$');
vectorList[count].regPrice = stof(input.c_str());// Casts string to a float
// Read sale price
getline(nameFile, input, '#');
vectorList[count].salePrice = stof(input.c_str());
Pastebin Links : [URL] ....
View 2 Replies
View Related
Jul 14, 2014
I have problem with string compare. I want to compare the string user input with a string in binary. And I don't know how to do it. Problem in function login();Here is the code: And you also can download file in attachment too..
Code:
#include<conio.h>#include<dos.h>
#include<stdio.h>
#include<process.h>
#include<string.h>
char nsb=1;
char ch, password[20], passlogin[20], inputpass[20], checked[20];
FILE *fp;
}
[code]....
View 1 Replies
View Related
May 29, 2014
I wrote a program that reads a list from a file and stores it in a string type vector. Now, I want the user to input a word so that the program can search the vector to see if that word already exists. I have used every possible way of reading input from the console and storing it in order to compare with the vector but it never results in a match. When I print the input string and the vector string they are exactly the same thing (or at least print to the console as if they were). I've tried using getline; using cin direct to a string var; using cin to a char array and then casting to string using string str(arr); I even added a newline at the end just in case and STILL I cannot get a match.
vector <string> currentSet; //read a list in from a file and has 9 items in it
cin.ignore();
string line;
getline(cin, line);
if(line == vector[0]){//if printed to console line is HEAT and vector[0] is HEAT
cout<<"match"<<endl;
}
View 3 Replies
View Related
Dec 3, 2014
write a program that prompts the user to input a string and outputs the string in uppercase letters. (Use a character array to store the string.) Does this follow the criteria? This program is very similar to one I found on these forums but I have one problem, it outputs everything backwards! EX: dogs will output to SGOD. What I need to do to make it output correctly, I think it may have to do with getline?
#include <iostream>
#include <cctype>
#include <cstring>
using namespace std;
int main() {
char let[100];
cout << "Enter what you would like to be UPPERCASE: ";
[Code] ....
View 2 Replies
View Related
Jul 23, 2013
I need to ask the user to input a number, and then put the number in a string array. so for example if the size of the number was 4: example 1200
i would make him put the number like that
for (i1=0; i1 < 4; i1++)
cin >> Num1[i1];
where Num1[] is a char array type. yes and it works.
But my question is what if I do not know how many numbers will the user put, how do i extract the size of the total characters existing in a number?
View 5 Replies
View Related
Nov 8, 2014
I'm trying to code the program that will store item data.And I'm having problems to receive user-input string to array.
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <string>
#include <iomanip>
using namespace std;
[code].....
View 6 Replies
View Related
Oct 25, 2013
I have been reading up on arrays and string array. I created a string string text[0] and it is defined by user input. I am trying to sort the input. I want Michael to read Macehil.
When I wasn't using an array and just a string I did this:
return sort(text.begin(), text.end()); a
And it worked fine. Do I need to change my string into a char? If so, would I static cast that?
View 9 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
Feb 7, 2013
I am stuck in this program, Be given a string of chars, where each single char belongs to the following alphabet: a..zA..Z0..9 (So, in the string there are only lowercases, uppercases and digits. No blank, no comma, ...). For every char of the given alphabet, count how many times in the string
1-- the char belong to a sequence of identical chars whose length is at least three (i.e.: in the string cc74uyrpfccc348fhsjcccc3848djccccc484jd for three times the character 'c' satisfies this condition)
Code:
#include<stdio.h>
#include<conio.h>
#include<string.h>
[Code]...
2-what is the longest substring of characters strictly rising interm of ASCII code(the following is greater (>) of the previous)
3- what is the longest substring of successive characters interm of given string rannge (i.e.: fhkjshdfruytyzABCDEfglsj => 7)
View 1 Replies
View Related
Oct 15, 2013
Here's what I have to do: Using strcpy() pass it two strings of characters (such as a name like John Johnson) and copy the second string into the first, in reverse order, and print both. Write a main to exercise your function and to illustrate that the function is working properly.
View 4 Replies
View Related
Jan 23, 2015
I have this:
void reverse(char *in, int isize) {
char t = 0;
for( int a = 0; a < isize / 2; a++ ) {
t = in[a];
in[a] = in[(isize - 1) - a];
in[(isize - 1) - a] = t;
[Code] .....
s:
float:123456
s:123456
The function i made is working fine, exacly how cout << float is working. The problem is i think my function is not really effective. By that i mean my function is doing alot of useless stuff to do what i want and there have to be a better way.
The rule is that you can not use other functions, only playing with loops and operators.
View 5 Replies
View Related
Sep 30, 2013
How would you search in a vector array from a user input string?
ex: user input : "Hello"
output: search vector array and find the line that has the string "Hello" and output the array "Hello" is on?
View 1 Replies
View Related
Sep 20, 2014
I'm trying to create a program that counts the amount of alphabetical characters, numbers, blanks and total amount of characters in a string a user gets to enter. The user also quits the program manually by pressing CTRL + D (in linux terminal).
These are the results I get from the code so far by typing in "hello cplusplus 123 c++" CTRL + D.
The string had:
Alphabetical characters...: 0
Numbers...................: 0
Blanks....................: 3
Total amount of characters: 20
So I can't seem to find a way to count the amount of alphabetical characters and numeric characters.
Here's my code:
#include <iostream>
#include <string>
#include <iomanip>
#include <cctype>
#include <sstream>
using namespace std;
int main() {
string tecken;
[Code] ....
The reason why I declared cctype is that I think I should use the isalpha, isdigit and isblank functions but how I am supposed to do that.
View 1 Replies
View Related
Nov 19, 2013
I just i would like to know how to record a string from a label to an array string ?
string[] stringArray = labelone.Text
View 1 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
Mar 29, 2014
Ok so I searched this site, and google string arrays, but I couldn't find anything on how to create an array to accept string input. In other words the strings are unknown, until the user inputs them..
so code would say input a name..user enters Tom, and its inserted into the array.. and if another name is entered ..lets say Lisa..Lisa is added to the array..so now in the array we have tom and Lisa..
Everything I read only shows the array already having the strings declared...
string n;
string name[n]={};
View 1 Replies
View Related