C :: How Getline Function Works
May 15, 2013
How the getline function works.
Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main() {
[Code] ......
getline(&inputstr, 128, NULL); getline gets a line in a data file.
I assume that inputstr[128] is the name of the file? why is the number of the array in the getline function....
View 7 Replies
ADVERTISEMENT
Mar 10, 2013
i want to write an array sorting program that works with recursive function.but i can not do it.
my algorithm
Recursive
find the min value
find the min value and save it
find the min value and save it and remove it from loop
put the rest in loop again
find the min value again
..
...
i didnt write the function because i dont know how will it work
Code:
#include<stdio.h>
#include<stdlib.h>
#define s 5
void main() {
int a[s]={25,3,2,4,1},c[s]; // c[s] for new sorting
int i,ek,j; //ek = min
[Code]....
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
Nov 1, 2014
I was able to get this program running. Now I working on taking one of my member functions and turning it into a standalone function. I choose the create_board() function. Yet, if I declare it in my header file or my main.cpp it cant access any info from the original member functions?
main.cpp
#include "ticTacToe.h"
#include <iostream>
#include <limits>
[Code]....
View 2 Replies
View Related
Feb 26, 2013
I have just started working through "Jumping into C++". I am at the section on appending strings. The tutorial mentions the getline function but I can not seem to get it to activate. There is no mention of any other inclusions.
Code:
#include <iostream>
#include <string>
using namespace std;
[Code] ....
I note that the getline function color remains black while other functions are green. I presume this means that Codeblocks has not associated it with any of the listed header files. Has the tutorial omitted this detail?
View 4 Replies
View Related
Sep 11, 2014
I am reading a file of text. I want to read in every word, but no spaces or newlines. "word" is a string, and "c" is a char (used for getting rid of white space. The problem: I can get rid off spaces perfectly, but newlines remain in "word" if it comes before the terminating character ' '.
My code:
while(infile.good() && !infile.eof()) {
while(infile.peek() == ' ')
infile >> c;
while(infile.peek() == '
[Code] .....
View 3 Replies
View Related
Feb 4, 2014
How do I use a string in place of a character in the function getline?
For example,
getline(infile,lines,'}');
This works fine, but I want the delimiter of } to be "};", but I can't do this as it only takes in characters, not strings.
I want:
getline(infile,lines,"};");
Any way to get around this?
View 1 Replies
View Related
May 28, 2013
I came across a strange problem while writing a program using the getline function. I understand that when using the getline function an input string will terminate when the "enter" key is pressed and in the following program it works as one would suspect.
#include <iostream>
#include <string>
using namespace std;
int main() {
string str;
cout << "Enter: ";
getline(cin, str);
cout << str;
}
However when I use it in this next program (below) that I have been working on it will only terminate after pressing the "enter" key if the first character is a number, otherwise it will not terminate. So the question is: How do I get it to terminate the string regardless of the input order?
#include <iostream>
#include <string>
using namespace std;
int i = 0, j;
int numValue;
[code]....
Additional Info: the purpose of this program is to change a character, which is extracted from a string, into an equivalent numerical value, if the character is an integer, and assign it to an int variable. I plan on eventually adapting it to return the correct value of a multi-character integer such as 123.
View 2 Replies
View Related
Aug 10, 2014
Taken from Accelerated C++ book, I modified those code to use getline() instead of std::cin >> only to find out that the output has extra line. Why is that so?
#include <iostream>
#include <string>
int main() {
std::cout << "What is your name?" << std::endl;
std::string name;
[Code] .....
What is your name?
Naruto
*****************
* *
* Hello, Naruto
*
* *
*****************
Notice one asterisk after the greeting where it should be in the same line as the greeting.
View 4 Replies
View Related
Apr 19, 2013
I am trying to create a program that reads data about different songs in from a file and displays the total length of all the songs and the average rating of all of them. Here is an example of the data that I would be reading in:
Just Give Me A Reason|P!nk Featuring Nate Ruess|4:22|4.0
When I Was Your Man|Bruno Mars|3:33|3.5
Thrift Shop|Macklemore & Ryan Lewis Featuring Wanz|3:55|4.5
I think my program is close to being done but for some reason it is not returning the correct length and average rating of all of the songs.
#include <iostream>
#include <string>
#include <fstream>
#include <cstdlib>
using namespace std;
// Structure declaration
struct Song {
[code]....
View 1 Replies
View Related
Mar 11, 2014
I have a big problem with a function, I wrote this function in order to get a line from an HTML (Or XML) file, until a specified delimiter (not always or
... It can be everything..)
Here is my code :
public static String GetLineUntilChar( String url , char delimiter , String postData, String referer, String cookie ) {
try {
Uri uri = new Uri("http://127.0.0.1//site.html");
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(uri);
request.ContentType = "application/x-www-form-urlencoded";
if (!String.IsNullOrEmpty(referer))
request.Referer = referer;
[Code] .....
View 14 Replies
View Related
Jan 24, 2012
Hey I am trying to use the getline() function to read a line from a file. For some reason Visual Studio 2010 gives me the following error. "No instance of overloaded function "getline" matches the argument list". The piece of code that produces the error is in a class in a separate .h file and is executed as a method of the object. I'm almost certain it has something to do with either the compiler thinking I am calling another getline in a different namespace or my parameters for the function are incorrect. Here is the code:
Code:
#include <iostream>
#include <string>
#include <vector>
#include <fstream>
using namespace std;
class InsultGenerator
[Code] .....
View 1 Replies
View Related
Jun 4, 2013
I have written a C++ program I have multiple of CSV file used as input, which I open one at a time and close it after extracting data to a output file which is the only file.
I run getline(inFile,line);
outFile << line << endl;
I run this code, and only part of it is goes to the output file I got, also have spacing randomly to specific file and inconsistent
But when I slower the code, like system("Pause") in the loop, I can get extract what I want perfectly....
Is my program running to fast, why getline would be skipping part of what things I want?
View 11 Replies
View Related
Apr 30, 2013
I'm having a really dumb moment and I cant seem to remember how an if statement works, with only the variable name as the condition..
Code:
if(var){
//then do something
}
View 7 Replies
View Related
Oct 5, 2014
My code has been acting odd. I made a function that layers my art resources but only the last tile of my art resource acts the way it should. My character goes behind and in front of the last tile and gets printed correctly. Strangely its really exclusive to the last tiles I print. What I need is to figure out in step by step order what going on with my code sample below and be able to layer the resources.
Here is a small sample of my main function. This is how I do my rendering.
Code:
Int main (int arc, char* args[]) {
//Move class
Move character;
//Class Tile & Side Tile
Tile *tiles [TOTAL_TILES];
[Code] ......
View 14 Replies
View Related
Apr 27, 2013
This is simple recursive solution of Fibonacci number:
Code:
int fibo(int n)
{
if(n<=1)
return 1;
else
return fibo(n-1)+fibo(n-2);
}
Now the recursion will generate a large recursion tree, like if n=5, 5 will call (5-1), (5-2) or 4,3 . What I want to know is, will fibo(n-1) will be called 1st go all the way to the base case 1, then do the summation or fibo(n-2) will be called right after fibo(n-1) ?
View 6 Replies
View Related
Dec 15, 2013
i want to ask how does malloc works on an array for instance can i dao this
Code: p=100;
int array[10];
array = malloc(p * sizeof(int));
then will the size of int be 100 so will i be able to do like
Code:
array[11] = 12 ; ???
also if i have a 2d array how can i use malloc on it and how does this works with pointers????
View 2 Replies
View Related
Oct 25, 2014
I know how exception handling works, but how should I actually use it in action? Let's say I have something like this:
Class X
{
public:
X();
//Pre-condition: example can't be 3.
void number(int example);
[code]......
If I know that the exception can only happen at the beginning of the function, is it okay to catch it right away?
View 7 Replies
View Related
Apr 24, 2013
I have this following piece of code:
int id = 5;
const char *ptrId;
ptrId = ( int_to_str(id) ).getPtr(); // Works in Solaris well
But the above code prints some junk values when I run the same on Linux machine. But the following works well on Linux:
String temp = int_to_str(id);
ptrId = temp.getPtr();
The prototype of getPtr() is:
const char* String::getPtr() const
View 2 Replies
View Related
Apr 20, 2013
Okay so I am programming an 8051 variant to interact with an SD card. In a separate initialization routine in SD_CARD.c I pull out the vital information of the card into global variables. I then call Menu() which is in another source file but includes a header file with all of the variables declared as extern. Now here is the weird, part this is from my Menu.c
printf("%u" , VOLUME_1_SECTOR);
if(VOLUME_1_SECTOR==16384)
printf("Correct");
else
printf("Incorrect");
Now the output of the first printf is 16384 but the conditional evaluates to false. If I put this code in SD_CARD.c (Where VOLUME_1_SECTOR is defined) the conditional evaluates to true. I am confused why the printf works correctly in Menu.c but not the conditional.
View 2 Replies
View Related
Nov 15, 2014
when i was finding a way to concatenate three integers in C, I came across a snippet like this
Code:
#include<stdio.h>
#define cat(a,b,c) a##b##c
int main()
{
int d;
d=cat(1,2,3);
return 0;
}
How the macro works here? I am unable to understand the function '#' plays in the macro.
View 1 Replies
View Related
Sep 8, 2014
I tried writing a program for finding the area of a circle, and I'm wondering why it only works when i define it as 'float', but not as a double?
Code:
#include <stdio.h>
#define pi 3.14159
double
main(void)
[Code] .....
View 3 Replies
View Related
Mar 15, 2013
Assignment: Take an integer keyed in from the terminal and extract and display each digit of the integer in English. Ex. 932 --> nine three two
Code:
/*This program takes an integer keyed in from the terminal and extracts and displays each digit of the integer in English.*/
#include<stdio.h>
int main(void)
{
//DECLARE VARIABLES
int num;
}
[code]....
I don't know how the program works if the integer is more than one digit.
View 1 Replies
View Related
Apr 22, 2013
I have a question about a dynamically loaded library I am using. I have called it SqlWrite, it is for connecting and writing to a Microsoft SQL server DB. I have a function in it that is defined as:
extern "C" {
__declspec(dllexport) RETCODE __cdecl SqlExecS(SQLHANDLESTR* h, LPCSTR Statement) {
RETCODE RetCode;
std::string input = Statement;
std::wostringstream conv;
conv << input.c_str();
std::wstring wCmd(conv.str());
[Code] ....
I then load it in another MSVC project as:
#ifdef WIN32//C:UsersaDocumentsVisual Studio 2012ProjectsGetPageSourceDebugGetPageSource.dll
#pragma message("WIN32 is defined")
#ifdef _DEBUG
SetErrorMode(0);
[Code] ....
As you can see, inside the prototyped function "SqlExecSP", I cout (or, rather, wcout for wide characters) the sql statement i am running, and the return code of the sql statement. a sql return code of "0" is equivalent to "SQL_SUCCESS". Then, I cout " got player again " after executon of SqlExecSP alias SqlExecS.
This usually works, and gives me following sample output:
exec SELECT [PlayerID], [FirstName], [LastName], [TeamID] FROM [soccer].[dbo].[Players] WHERE LastName = 'Abdellaoue' AND FirstName = 'Mohammed' retcode 0
RETURNING FROM EXECSP NOW!!!
got player again
However, sometimes, the program crashes somewhere between outputting "RETURNING FROM EXECSP NOW!!!" and outputting " got player again ", i.e. the output is then:
exec SELECT [PlayerID], [FirstName], [LastName], [TeamID] FROM [soccer].[dbo].[Players] WHERE LastName = 'Abdellaoue' AND FirstName = 'Mohammed' retcode 0
RETURNING FROM EXECSP NOW!!!
As you can see, it doesn't output the next line " got player again ", because it somehow crashes in between.
However,t he only line that should be executed between this, as far as I can understand, is the actual return of the DLL function SqlExecS, prototyped as SqlExecSP in my calling code, i.e. the only line that should be executed in between is:
return RetCode;
However, somehow, this fails, even though RetCode is "0", as I can see at the end of the output
exec SELECT [PlayerID], [FirstName], [LastName], [TeamID] FROM [soccer].[dbo].[Players] WHERE LastName = 'Abdellaoue' AND FirstName = 'Mohammed' retcode 0
Now, why sometimes this crashes, and sometimes this works. I.e, I can sometimes call this function x times, and it doesnt fail, outputting " got player again " right after the calls, and sometimes, it fails somewhere in between, at call x, y, or z, i.e. sometimes i can execute it ten times successfully and sometimes i can't, even though the return code is still 0, and it just fails somewhere in between. I am not sure if it has to do with the call being a call to a dynamically loaded DLL function, but I can't see where else the error is.
Why this can be failing, and at different, seemingly random times?
View 3 Replies
View Related
Feb 20, 2013
i've got an assigment that requires me to overload some operators and add some objects together.
I will show the code and explain as good as I can.
void SArray::operator+= (const SArray &obj)
{
Sphere * tmp_arr;
tmp_arr = new Sphere[obj.antalobjekt+this->antalobjekt]; //antalobjekt = //Gets amount of elements in the arrays.
[Code]......
m_arr is the inner array for storing elements, do ask if something is not clear enough. The copy constructor works, so i have not included it.
View 1 Replies
View Related
Jun 24, 2013
My program works primarily by receiving user input however; using 'char' and 'fgets' i have to stipulate how many characters i want assigned, and this isn't practical for what I am after. Example below.
Code:
char example[50];
printf("What colour is the sky?
");
fgets(example, 50, stdin); Is it possible that the assigned number (in this case '50') is determined strictly by the user input?
for example, user input is Blue then 50 is then 5?
View 9 Replies
View Related