C/C++ :: Sscanf Not Storing Variables Correctly
Jan 23, 2015
I'm trying to read in 3 values from the first line in a file. I want to take those 3 values and store them into 3 variables.
char buffer[100];
if((fgets(buffer, sizeof buffer, in) != NULL))
{
printf("current buffer is %s", buffer);
double tmpPrePrice, tmpDoorPrice;
int tmpTicCount;
int a = sscanf(buffer, "%d %d %d", &tmpPrePrice, &tmpDoorPrice, &tmpTicCount);
printf("applied %d variables %d %d %d
", a, tmpPrePrice, tmpDoorPrice, tmpTicCount);
}
The input is
15 25 200
And the result:
current buffer is 15 25 200
applied 3 variables 15 0 25
any reason these aren't being stored properly?
View 3 Replies
ADVERTISEMENT
Mar 10, 2013
Well I have my program running and the Variables are not passing correctly and the return statements are not returning correctly. Here is the parts that are not working.
#include <iostream>
#include "fight.h"
#include <time.h>
#include "player.h"
#include "stdlib.h"
#include <cstdlib>
using namespace std;
combat A;
combat::combat(void)
[Code] ....
View 11 Replies
View Related
Mar 16, 2015
how to read a csv file where the data is seperated with commas so the csv file looks like:
programming,03,60,141,01,W,2015
programming,03,60,141,30,W,2015
Algebra,03,62,102,02,S,2013
Religion,08,98,938,20,F,2014
So i need the name of the course in one variable, the course code (ex 0360-141-01 for line 1) in another variable and the term (ie W2015 for line1). So i got the name done but cant figure out the course code since i need more than one value that is seperated by a comma. my code is:
#include<stdio.h>
#include<string.h>
typedef struct CourseInfo {
int courseID;
[Code].....
View 2 Replies
View Related
Feb 9, 2014
I am using Visual C++ to write an app. One problem is that the application will allocate a lot of object instances of a specific class CMyObject, maybe 400, 000. This will cause "Out of memory" error when total allocated CMyObject reaches 400,000.
To solve this problem, I just wonder when a new CMyObject instance is created, is it possible to specify so that the instance will allocate on a disk cache, or file mapping instead of the memory space?
View 3 Replies
View Related
May 30, 2014
I have a problem in my code and after hours of looking I really cannot find out what is the problem.I use scanf to to parse lines of a file, let me copy only the important part:
Code:
/*File read...everything is fine*/
int seq_1, seq_i2;
int len;
while(fgets(&line[strlen(line)], line_length, file)!= NULL) {
}
[code]....
if (num_sc ==4) //the wanted format, do something So I am allocating all variables, and giving to sscanf pointers, so I really don't get where the segmentation fault appears.I checked with the debugger, the first line of the file is read, but at the second it crashes at the line of sscanf! The two lines don't have the format I want in sscanf but therefore I check if this number is 4 and then do other stuff to the data...
View 9 Replies
View Related
Jan 23, 2014
I am beginner at C and I was working on a program where I have to read in a line such as Digit, String, Float. The string can have any amount of spaces between it. and each input is separated by a space character.
The input is of the format:
10000000000 hello my n ame is 30.2
So I used fgets(x,100,stdin) to read the line in.
So I need to read that line into an int, array, and float.. so I was thinking of using this:
sscanf(x, "%d %[^/n] %f", &number, &username, &numberfloat);
Now, obviously I can't use the /n to read in the username with spaces because I need to read the 30.2 into a float variable.
View 5 Replies
View Related
May 21, 2014
I'm new to C/C++. I am attemping to use fgets and sscanf to read a line of input, and confirm it is a positive number.My code works great, except for the case of a negative number. When I enter a negative number, my while loop seems to run infinitely, with stdin providng the same input over and over again.
Here's the code snippet:
Code:
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#define TEXT_LEN 64
void foo() {
char* inStr = (char*)malloc(sizeof(char)*TEXT_LEN);
memset(inStr, 0, TEXT_LEN);
}
[code]....
View 9 Replies
View Related
Feb 17, 2015
so i have this problem with my code ( not running)i want to read ( 2 double numbers x,y) and ( one integer z) then calculate reminder of x and y ( after they both converted to integer) and the formula : x^2 + y^2 + z^2and : x^z + y^z
here is what i came up with :
Code:
// compute.c
#include<stdio.h>
#include<math.h>
int main(void)
{
[Code]....
it also says in q : be sure to test the user input to make sure x,y,z are positive . any negative or zero should not be accepted and must print error msg ==> do i have to have an if statement here ? or the while loop is enough ?
View 9 Replies
View Related
Jan 23, 2015
I'm working on a school assignment that asks us to make code calculating the quadratic formula.
Code:
#include <stdio.h>
#include <math.h>
int main(void) {
double a = 0;
double b = 0;
double c = 0;
double discriminant = 0;
double solution = 0;
double solution2= 0;
}
[code]....
View 4 Replies
View Related
Jul 19, 2013
I'm writing a version of the classic Snake game. However, my board is not printing correctly. The right hand border is in the incorrect location. Also, when I randomly generate where the food ('X') is located, it only generates on the edges of the boundaries.
#include <iostream>
#include <cstdlib>
using namespace std;
const int ROWS = 21;
constint COLS = 61;
[code].....
View 5 Replies
View Related
Feb 7, 2015
My program isnt averaging correctly.
#include <iostream>
#include <string>
#include <iomanip>
#include <cstdlib>
using namespace std;
struct nameGPA {
[Code] .....
View 4 Replies
View Related
Nov 2, 2014
When I try to open the cpp source in Visual Studio, it just opens a blank notepad window. The cpp file says its 4 kb and there was code in it.
View 5 Replies
View Related
Nov 26, 2013
#include <iostream>
#include <cstdio>
#include <fstream>
#include <string>
#include <iomanip>
using namespace std;
const int SZ = 55;
[Code] ....
View 1 Replies
View Related
Oct 28, 2014
I have an assignment for class .. It works, the do-while loop isn't working correctly. Once I am doing inputting information for any employee It should ask to continue. It doesn't, It skips that loop and prompts to enter the type of employee again.
#include <iostream>
#include <iomanip>
using namespace std;
int main( ) {
char empInput;
char continueResponse;
[code].....
View 3 Replies
View Related
Mar 5, 2015
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
[Code]....
It compiles just fine and it will add new entries and print the entries just fine but when I go to delete an entry it will delete it but it will mess up the one before it.
View 8 Replies
View Related
Dec 14, 2013
I am writing a program that manages a group of tool bins. This group is handled as an object that is an array of two element structures called InvBin. I initialize the bins with data from a file which contains the descriptions and initial quantities. I also have functions to add or subtract items from a bin and a function to display a report of the description and quantity of all of the bins.
The add and remove functions work correctly based on the cout statement in the functions, however when I display the report, it displays the initial quantity instead of the new quantity. In addition, when I use the add and remove functions again on the same bin, they use the initial quantity.
These are the add and remove functions and the report function from the main program.
Code:
//Adds an item to a bin
void addItem(HANDLE screen, BinManager tools, int &count) {
int binNum;
int addNum;
system("cls");
[Code].....
View 2 Replies
View Related
Jul 24, 2013
I've been working on this program to create a simple desk calculator for a school assignment, and I managed to finish. All we had to do was add, subtract, multiply, and divide positive integers - and I was able to do that just fine. This program got me thinking though, because I do not know how to write commands to multiply/divide negative numbers.
In fact, when I divide a number like 21 by 4, it comes out to 5 because I don't know how to allow it to compute remainders (which wasn't a requirement for my program). This intrigued me so I've been trying to figure it out for the last few days but to no avail. Here's my code:
Code: void flush_buffer(){
int ch;
while ((ch = getchar()) != '
' && ch != EOF);
[Code]....
And just know that my code works perfectly fine, I'm not here for troubleshooting it. I just want to know what I can change to allow negative values to be correctly computed.
View 12 Replies
View Related
Feb 14, 2013
Code:
public void dam_data_setup() {
// fill list
damgtype.Add( den1);
damgtype.Add( den2);
damgtype.Add( da1);
damgtype.Add( da2);
damgtype.Add( db1);
damgtype.Add( db2);
[Code] .....
This is a genetics program and is to parse the source array and write all possible combinations to a new array. All sections but dilute work correctly. For some reason the dilute's Boolean is not testing true when it should. This is causing data corruption.
View 3 Replies
View Related
Dec 21, 2013
the info gotten from the text file are all wrong, i can't find any problem with my code,
Code:
#include<stdio.h>
FILE *f;
typedef struct prd {
char name[30];
char code[30];
char idt[30];
float price;
[code]....
View 7 Replies
View Related
Oct 23, 2013
I am trying to use libXl to output text from a C++ program to an Excel file. The problem is coming with this library function:
bool writeStr(int row, int col, const wchar_t* value, Format* format = 0)
Writes a string into cell with specified format. If format equals 0 then format is ignored. String is copied internally and can be destroyed after call this method. Returns false if error occurs. Get error info with Book::errorMessage().
If I give input as a string literal like "Hello World" it is displayed correctly. However, if I try to give input as a variable of type const char*, it displays garbage.
Following is my code. MyCompany::company is a QString.
const char* companyName = MyCompany::company.toStdString().c_str();
sheet->writeStr(4, 0, companyName, companyFormat);
View 3 Replies
View Related
Mar 18, 2014
Ii need an output like this...
#include <iostream>
#include <iomanip>
#include <stdlib.h>
#include <ctype.h>
#include <locale>
using namespace std;
int EquationNum (int Num1, int Den1, int Num2, int Den2);
[Code] ....
View 9 Replies
View Related
Jun 20, 2014
I am getting this weird error when calculating sums. Here is what my program does: it generates two random numbers and then randomly puts either an addition or subtraction sign between the two and then I am asked to enter what the correct answer for that question is. But on some of the subtraction questions when I give the answer it says I got it wrong after which the program shows the right answer. What's confusing is that the answer the computer calculates is the same that I entered. Anyways the complete program and a screenshot of the console window which shows the error are below. The function in question here is decAddSub at line 54. The error is shown in the first sum in the output window. It doesn't affect addition problems and only affects some of the subtraction problems.
#include<iostream>
#include<stdlib.h>
#include<time.h>
#include<random>
[Code].....
View 2 Replies
View Related
Nov 17, 2014
I have a program that's not doing what I want it to do. This is the assignment:
The nth term of the sequence of triangle numbers is given by, tn = 1/2 n (n+1); so the first ten triangle numbers are:
1, 3, 6, 10, 15, 21, 28, 36, 45, 55,...
By converting each letter in a word to a number corresponding to its alphabetical position and adding these values we form a word value. For example, the word value for SKY is 19 + 11 + 25 = 55 = t10. If the word value is a triangle number then we shall call the word a triangle word.
the 'words.txt' has a bunch of words in this format:
"ABSTRACT"
"YOUTH"
there's about 1000 words in that format.
And for any triangle words I find, I put it into another text file which I called triangle.txt.
Now for my program, it pulls the 'words.txt' file just fine but it doesn't recognize any of the words in the file as triangle words.
#include <iostream>
#include <fstream>
#include <string>
#include <sstream>
#include <cmath>
#include <cstdlib>
int getCharValue(char a);
double sum1(std::string name);
[code]....
View 2 Replies
View Related
Sep 10, 2014
I'm making a 2D Terraria-like (side-scrolling RPG with destroyable blocks) game with SFML 2.1, and I have no clue how to correctly implement collision detection. Meaning that I have been successful at making it work, but it's not clean and definitely not dynamic.
By example, I mean design wise. What parameters should collision functions take? Where should these functions be located? The same 2 questions for collision reaction. Since I have multiple things that need to collide with each other (player with enemy, player with items, enemy with items, enemy with enemy, player with player, etc...), and the world has to be treated differently, things get complicated. There is no block class, just a vertex array, so collision with the world is based purely on coordinates taken from that array in the world class, which, considering it's the world and not something like a player or an enemy, doesn't inherit from the same base class that they do.
View 7 Replies
View Related
Feb 1, 2015
I am struggling to understand what the correct way to initialize instances of a class for object orient programming is.
If I am not mistaken, there are 2 methods to initialize an instance "instance_name" of CLASS1 by calling the constructor:
(1) CLASS1 instance_name(argument);
(2) CLASS1 instance_name=CLASS1(argument);
As it turns out, I cannot use method (1) when I try to initialize a private instance inside another class. But I can use it in the main() code. I hope the code below will explain what I mean. Why only method (2) will work inside another class? Or is there another fundamental mistake I am making?
(BTW: I used CodeBlocks 13.12 with GNU GCC compiler for this example)
#include <iostream>
using namespace std;
class CLASS1{
[Code].....
View 6 Replies
View Related
Mar 15, 2014
I'm working on a homework problem where we are given a class of functions to traverse a maze and must use those functions to recursively find the end.
The problem I'm having is when the function backtracks it doesn't reset the whole path it came from.
Here is my function that I have created
void solveMaze(Maze M, int path[][MAX])
{
// << overwritten to print the maze out
cout<<M;
[Code].....
View 7 Replies
View Related