C/C++ :: Program To Emulate Shopping List - File Not Reading Correctly
Jan 28, 2015
I have written an program to emulate a shopping list. Everything works fine, until I try to read in from a file. Here is my read and write code (the write code looks like it is working ok, but I figured I would include it just in case), and an example of what happens to a list that has been written, then read back in.
I took this list, and wrote it to a file, then read that file back in. The .txt file looks like everything was written out properly, but when I try to read that exact same list back in, this is what I get:
View 10 Replies
ADVERTISEMENT
May 20, 2013
We are supposed to split this lab into 3 files a .h and 2 .cpp's. The code I'll be talking about is going to be directed at the main.cpp file. Basically we are supposed to be able to enter as many classes into the program and when you are done the program is supposed to save the contents into a .dat file. This lab is working off of an older lab so I already have a .dat file I am trying to test with this program by reading it into my program when I first run it I prompt if the user wants to load previously entered courses and if they reply with a 'Y' the program will load the contents of the .dat file. Problem is right now when I load from the .dat file only the first class is correct. Every other class after that is just giberish. I've tried everything but can't get the restore part working. I also cannot get the save to .dat portion of this lab working either.Here is my code:
#include "Course.h"
#include <iostream>
using std::cout;
using std::cin;
using std::endl;
using std::ios;
[code]....
View 1 Replies
View Related
Jan 9, 2015
OK I'm making a simple program to make a shopping list. I would like the program at start up to load the previous shopping list that was saved as a text file. The format is one line per entry which consists of the category or Isle, and the item description. Here's an example:
3 Dog Food
Produce Sweet Onions
I reading the first word, and then I want to read the rest of the line which may have more than one word... the problem is my code hangs... or goes into the old infinite loop. It doesn't see the end of file.
Here is my code:
void
addItemsFromFile(vector<item> &shoppingListVector) {
string word;
char buf[30];
if (fileExists("shoppinglist.txt"))
[Code] .....
View 3 Replies
View Related
Jan 28, 2014
I am new to programming, and trying to make a C# Windows Forms program, to easy our weekly dinner-planning.
I would like to make something the work like the attached Picture.
A combobox for each day, where i can select a stored dinner with recipe.
To the right it will automaticly make a shoppinglist, which I can print.
If for example i have selected 2 dinners which both needs 500 grams of meat, i should automaticly write 1000 grams of meat.
View 6 Replies
View Related
May 5, 2013
Why program crashes when reading linked List from file?
#include <iostream>
#include <fstream>
using namespace std;
struct link{
int data;
link* next;
[Code] .....
View 4 Replies
View Related
Dec 5, 2013
So, I'm making a program in C++ that emulates a full screen console... I used SetCurrentConsoleFontEx to set the font to 12x16 but it sucks...
Anyway, I read here: [URL] .... that also the console font can be changed, but I can't make it work! How can i set the font to Lucida Console? This is the function I use to change font size:
void setFontSize(int x, int y) {
PCONSOLE_FONT_INFOEX lpConsoleCurrentFontEx = new CONSOLE_FONT_INFOEX();
lpConsoleCurrentFontEx->cbSize = sizeof(CONSOLE_FONT_INFOEX);
GetCurrentConsoleFontEx(GetStdHandle(STD_OUTPUT_HANDLE), NULL, lpConsoleCurrentFontEx);
lpConsoleCurrentFontEx->dwFontSize.X = x;
lpConsoleCurrentFontEx->dwFontSize.Y = y;
SetCurrentConsoleFontEx(GetStdHandle(STD_OUTPUT_HANDLE), NULL, lpConsoleCurrentFontEx);
}
View 2 Replies
View Related
Feb 13, 2013
how to structure my classes so that they all "wire together" and inherit the proper functions and data that I want them to. I want to have a Car.c and car.h which are "wired" with body.c/body.h, which in turn is wired with a frame.c/frame.h, wheel.c/wheel.h, and driver.c/driver.h.
Each part of the car holds some type of data, mostly ints: The car has a (total) mass and car_name. The body has a mass and color. The frame has a mass. The wheel(s) have a mass and force. [There will be 4 instances of wheel] The driver has a mass and driver_name. Each of these functionalities must come from their respective .c and .h files, and be amalgamated in the car.c and car.h (which should contain all the functionality of its parts). Then, in my main test program, I am to make an instance of car and hardcode in its values of: mass (which comes from the total mass of all of its parts, this is where i start to lose it.
How will I access its parts' masses in the test program?), color (a character array), current position (an integer), current velocity (an integer), and current acceleration (which comes from the total newton force of the 4 wheels, again this is where I start to get very confused). (Then in the test program I am to print out a simulation of this car over a period of 100 seconds, and show its position, velocity, and acceleration at each point (based on the mass, total newton force, and starting position). This part is of course a simple while loop, and is somewhat trivial.)
From what I understand, I will need to allocate memory for all of the data each class holds. Then, using function pointers, as well as #include "X.h" , I will need to somehow link up all of the classes together so that car has access to all of the parts' functions which set, for example, the newton force of each wheel.
We have learned a slight bit about structs, malloc, sizeof, and pointers, but I have not used them to link functionality or data from separate classes together.
View 4 Replies
View Related
May 29, 2014
I am trying to open any selected textfile and have the text input be sent to a listbox... Originally I wrote this code for a textbox which worked great now that I am converting it to a listbox it doesnt seem to work so much. I left the default item names in order for better understanding of what is going on.
private void button1_Click(object sender, EventArgs e) {
if (openFileDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK) {
label1.Text = openFileDialog1.FileName;
listBox1.Items.Add = File.ReadAllText(label1.Text);
}
}
View 5 Replies
View Related
Nov 19, 2013
Code:
void loadData(fstream& fin, nodePtr head){
if (fin.eof()) {
return ;
}
else
[Code] .....
This is my function and it is not working.
View 7 Replies
View Related
Nov 17, 2013
Code:
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <iomanip>
#include "contacts.h"
using namespace std;
[Code] ....
Lines 29-32 I have a good feeling very wrong. Now I have never learned how to do this and my book covers nothing over this. I just took my final in C++ so this is not homework. I am trying to get better before Data Structures start next month.
View 3 Replies
View Related
Oct 15, 2013
I am trying to read in a text file and add strings from it word by word into a Linked List. I'm fairly new at C and don't quite understand pointers. I've had a few different errors just messing around with it, but now I'm getting an infinite loop within my main program.
Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
struct listNode { /* self-referential structure */
char data[50];
struct listNode *nextPtr;
[Code] ....
When I run my full code, it prints 12345ooooooooooooooooooooooo...etc. In my test file the first word is "Hello" so that's where the infinite 'o's come from. If the outer loop is the one that is infinite, then wouldn't the second while loop also execute more than once? What I mean is, why is the second print statement the only one that repeats itself?
View 5 Replies
View Related
Jun 1, 2014
I've been trying to write my homework assignment (a list of countries, their codes and their capitals) and I've done most of it but I'm stuck at this: I have to open a file, read it and if there are data, add them to the list. So far I've created an element of the structure, queue list, printed the list on the screen and freed the memory. I thought that for reading the file and adding the data I could first open the file (of course) with fopen and after that use a for loop (i=0;i=!EOF;i++) to scan the whole file and fscanf(fp,"%s",result->country),etc in the loop to add the data to the structure of the element and finally insert that element to the queue list. However, when I try to do these operations, I only get to writing the name of the file and the program crashes.
Code:
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
struct List {
char country[20];
char code[5];
char capital[20];
struct List*next;
[Code] .....
View 1 Replies
View Related
Mar 26, 2015
I have a data file that looks like:
111111111
Lisa
Porter
3
ENEE 114
CMSC 412
ENME 515
333333333
Alex
Simpson
1
CMSC 412
***
In the form student ID and then first name last name number of courses and finally the course code(s). Im currently attempting to read these into linked lists with the following code:
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
struct CourseInfo {
int courseID;
char courseName[30];
[Code] ......
I get no errors or warnings however when i print this out i get:
111111111
Lisa
Lisa
Porter
3
ENEE 114
CMSC 412
ENME 515
And then the program crashes. So i know that for some reason the problem is the
fgets(rootPtr->FirstName, 22, p);
But I do not know why because it seems to work for the other fgets. I also want to know why the while loop never runs and it crashes instead.
View 14 Replies
View Related
Dec 1, 2014
I have to initialize my stack (done by linked list) and I have a txt file for that. It looks like
123 Mike Smith 3.5 BA
124 John Weasly 2.9 CD
Here is what I have done so far:
However, I cannot reach any result.
Let me update:
123 Mike Smith 3.5 BA
124 John Weasly 2.9 CD
Here is what I have done so far:
struct Record{ //struct
float number;
char name[10];
char surname[10];
float GPA;
char mark[SIZE];
Record *next;
};
[Code]...
View 1 Replies
View Related
Dec 26, 2012
I have problem reading CSV file, i found many solutions but i still got one problem. I need to read CSV file and then these values show in List Control and here i have problem. Idk how to display it in List?
View 4 Replies
View Related
Mar 13, 2015
I'm having a problem in my Library assignment, this section of my code is for reading in books saved in a 'book.dat' file on my desktop and inserting them into the linked list. It kind of works, but say if there is two books in the file, it only saves the second book twice.
eg in book.dat:
123 book1 Tolkien 2009 0
111 book2 Rowling 2009 0
So once these are read in, and I call my method displayAll(), it would display the second book twice..
void importFromFile(FILE *fp) {
struct book *aBook;
struct node *aNode;
aBook = (struct book *)malloc(sizeof(struct book));
[Code] .....
View 6 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
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
Feb 13, 2015
how to read file twice in a single program?
View 4 Replies
View Related
Jan 24, 2014
I have a program that makes a .txt file and I am trying to read it and it won't.
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
int main() {
string fershizzle;
[Code] .....
View 13 Replies
View Related
Mar 19, 2013
I'm working with parallel arrays, one handles the titles and the other handles the ratings. Now everything works fine if the title doesn't start with a number, but if I decide to use a number infront of the title, when it reads it from the .txt file it thinks it is part of the rating.
Here is what I mean:
1234567891011121314151617181920212223242526272829303132333435363738394041424344
LOADING DATA FROM FILE...
MENU
1. Add Movie
2. Print All
3. Exit
------------------------------------------------------
Enter 1-3 : 2
PRINT ALL
------------------------------------------------------
Hitch 98% FRESH
Happy Gilmore 99% FRESH
Transformers 2 100% FRESH
SpongeBob 10% ROTTEN
MENU
[code]....
As you can see, the '2' from 2 Fast 2 Furious went to the rating in SpongeBob.
Here is what it looks like inside the .txt file:
Hitch| 98Happy Gilmore| 99Transformers 2| 100SpongeBob| 102 Fast 2 Furious| 100
Here is my code:
void SaveToFile(const string titles[], const int ratings[], int count)
{
string filename;
ofstream outfile;
filename = "tomatoes.txt";
outfile.open(filename);
PrintToText(outfile, titles, ratings, count);
WriteLine(cout, '=', 54);
[code]....
View 3 Replies
View Related
Feb 15, 2014
I'm not sure why my program isn't reading data from the dat file. I'm not sure if it's because I messed up my pointers, or I'm missing something. I know we usually use Infile >> blah >> blah >> blah; to get the data from the file, but my instructor showed an example that had the struct read it for you in this format and I could just call it with pointers.
#include <iostream>
#include <iomanip>
#include <fstream>
#include <assert.h>
using namespace std;
using namespace System;
typedef int Integer;
[Code] ....
View 12 Replies
View Related
Jun 2, 2013
I have a procedure that prints the fields of ethernet frames and ip headers. I have an issue with the src mac addr being printed incorrectly and incompletely. The part in red is the trouble code.
Code:
1#include "sniffer.h"
2
3void print_headers(struct ethhdr * ethhdr, struct ip * iphdr){
4
5 char ipstr_src[INET_ADDRSTRLEN];
6 char ipstr_dst[INET_ADDRSTRLEN];
7 char macstr_dst[ETH_ALEN], macstr_src[ETH_ALEN];
[Code] ....
View 2 Replies
View Related
Oct 10, 2014
Why this program won't correctly print the proper totals on screen? See my code and the included screenshot.
Code:
#include <stdio.h>
int main(void) {
int total, first, second;
printf("Enter 2 numbers:
[Code] .....
View 4 Replies
View Related
Oct 3, 2013
I need to decrypt my text file and make it able to read in my program..
What I have programmed so far is to read the encrypted file, create a new file, decrypt it and read the newly created file..
I need to decrypt the encrypted file without having to create a new file that reads the decrypted text..
Well, Let me show you my code:
P.S Most of the include is not needed and I already know that Visual studio 2010 Windows Form Application CLR
Code:
#pragma once
#include "stdafx.h"
#include <iostream>
#include <fstream>
#include <conio.h>
[Code].....
You have my decryption code and the code I want to use..
I have uploaded the code for you to use on your computer because it is fairly hard for me to explain..
I want to be able to read the encrypted file in my program, without having to writing a new file to decrypt it..
Decrypted & Encrypted .txt file Included (And images)
--> Program .rar pack link <--
Build it with Visual Studio 2010
View 5 Replies
View Related
Jan 11, 2013
I am trying to create an on-line product store. I'm having trouble with setting up the code to correctly acknowledge quantity desired per product item in the shopping cart at check-out. I want to be able to capture multiple impressions/hits/clicks per item and increase the # in the quantity box each time someone clicks on the same item. For example, if product A is clicked 3 times, then the quantity in the shopping cart should show "3". If it's clicked 5 times, it should show "5" and so on ... However, right now, it's showing the product multiple times in the shopping cart with a quantity of "1" for each time the item was clicked. How can I correct this?
View 2 Replies
View Related