C/C++ :: How To Assign Values From Text File To Structure Of Arrays

Sep 5, 2014

have to do an election program in C.

There are 7 candidates and 365 votes in total. I need to do this using an array of structures. I need to read from a text file each of the names of the candidate and the number of votes they get. At the end i need to output the winner of the election.

Here is a sample of my code so far

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct candidates {
char name[20];
int votes;

[code]....

Here is a sample of my text file:

Robert Bloom
John Brown
Michelle Dawn
Michael Hall
Sean O’Rielly
Arthur Smith
Carl White

1 2 4 5 1 2 3 4 4 1 2 3 7 4 4 5 3 7 7 7 7 7 7 7 7 7

Each candidate gets +1 vote for their number electionCandidate[0] for each one he gets one vote and so on for the rest. 365 voters in total.

I was able to input the name for each Candidate from the text file. Now the problem is putting each vote to the corresponding candidate. Also any vote that is above 7 is a spoilt vote which i am trying to count in the above code. The code compiles but it crashes.

I am using while(!feof) but it seems that its not working or this is not the correct way.

View 6 Replies


ADVERTISEMENT

C++ :: Assign Values To A Dynamic Memory Structure?

Dec 27, 2014

I create some code that assign values to a dynamic memory structure. I'm not sure why one code works and the other crashes when it assigns a value.

Working Code

#include <iostream>
using namespace std;
struct WorldOjectCollisionMap
{

[Code].....

View 6 Replies View Related

C++ :: Reading Values In Order In File And Assign?

Apr 12, 2014

My game is a sort of RPG with stats, money etc. I just recently added a save/load system using writing to a file. I initially got writing to a text file to work, then I got loading to work too.I eventually was able to read numbers from the file and assign them to integer variables in order.

My issue was I wanted to check if a save file existed, if it did, load it up, if it did not, go to character creation. I had a lot of trouble with this and after trying different code snippets to work I finally got it to check if a file existed, and execute the appropriate code.

My issue now is my code USED to go through each entry and assign variables in order.

Like the first number in the text file would be for the variable money, and it would read it, assign to to int money and scroll to down to the next variable for player strength, assign to to playerstr variable and so on. After making the tweak for loading it no longer functions like this, and makes the last entry in the text file the value for everything.

Here's my code:

Save Code:

{
ofstream savegame;
savegame.open("C:/Sounds/savegame.dat", ios::trunc);
savegame << money << endl;
savegame << playerstr <<endl;

[Code]....

I barely get how this code works, how can I tweak it to go through the file in order and assign variables one at a time?

At the current moment, it assigned the playerstr value to both money and playerstr int. But the save file being created lists the correct values in order.

View 5 Replies View Related

C :: How To Assign String Hello To Structure Member

Feb 17, 2015

See the simple code below. The compiler gives message:

assigning to type "char[20]" from type "char *".

I've tried everything else that seems reasonable but none works. How can I assign string Hello to the structure member?

Code:
int main() {
struct strc1
{
char msg1[20];
char msg2[5];
}
talk;
talk.msg1 = "Hello";
}

View 5 Replies View Related

C :: Importing Text File To Structure

Nov 21, 2013

I am trying to take a Text File mydata.txt and import it into a structure however when I try to run this code it just closes out.

Code:
#include<stdio.h>#include<stdlib.h>
#include<string.h>
#include<ctype.h>
struct hard_disk{
char name[18];
double size;

[Code] .....

View 4 Replies View Related

C :: How To Do Arithmetic On Structure Members Read From A Text File

May 19, 2014

I am trying to use arithmetic on structure members read from separate text files into separate functions. I'm trying to do the math in the main function after calling the other two functions. I'm trying to divide nato_attack by pact_defence.

The warning I'm getting is: both are being used uninitialized in this function.

The goal is to read/write the info in the text files and to use them as any other variable, i.e. add, subtract, etc. in other locations in my code, i.e. functions, modules.

I "borrowed" most of this code from a youtube lesson and modified it for my needs.

Below is the contents of the two text files and my code.

This is for NATO
6
12 4th_mech_div mech div 14 16 7
12 5th_mech_div mech div 10 8 7
12 3rd_mech_div mech div 5 6 6
12 1st_mech_div mech div 4 6 7
12 2nd_mech_div mech div 12 14 7
12 6th_mech_div mech div 8 12 6

This is for Pact
3
10 1st_Guards armor div 12 8 6
10 2nd_Guards armor div 12 9 6
10 3rd_Guards mechanized div 10 9 6

Code:
#include <stdio.h>
#include <stdlib.h>
typedef struct {
char *unit_id;
char *unit_type;
char *unit_size;

[Code] ....

View 9 Replies View Related

C/C++ :: How To Structure And Write A Class For Text File Streams

Apr 20, 2014

I need to make a program which reads multiple lines from a text file and stores that information in a vector of structs(the vector class template also needs to be custom made).

One of my requirements is to have a class dedicated for I/O for the text file. At the moment I can't seem to get a way to input the data from a file into a vector from an InputOutput class. This is my code:

InputOutput.h

#include <ostream>
#include <fstream>
#include "Share.h"
#include <string>
#include "Vector.h"
class InputOutput {

[Code] ....

This is the menu selection function in the menu.cpp where i figured i would call the Input file and store it from case 1. I think I'm doing it wrong though. Is there a better way of doing this because at the moment i am getting some errors such as error LNK2019.

void Menu::UserMenuSelection() {
Vector<Share> shares;
std::ifstream infile;
switch (menuOption) {

[Code] ....

View 2 Replies View Related

C++ :: Using Arrays As Sources Of Data For Arrays In A Structure?

Feb 6, 2014

I define "Comwords" as a string, but apparently it takes the members as chars, then I can't set strings in a structure equal to the chars.

I see to also be having unknown problems with the ComMAL array and loading it values into another element of the same structure.

How to correct this? I was thinking of casting char elements as strings, but could find no reference in my library book regarding how to do that (lots on casting int's a doubles...)

Code:

int _tmain(int argc, _TCHAR* argv[]) {
int comm = 10;
int targ = 5;
int death;
struct AI_WORDS

[Code]....

View 2 Replies View Related

C :: Read Specific Values From A File And Store Them In Arrays

Apr 14, 2013

What i try to do is to write a code which reads some specific values from a file and stores them in an array. My File looks like this

Code:

XFOIL Version 6.96
Calculated polar for: Myfoil
1 1 Reynolds number fixed Mach number fixed
xtrf = 1.000 (top) 1.000 (bottom)
Mach = 0.000 Re = 0.200 e 6 Ncrit = 4.000
}

[code]...

View 11 Replies View Related

C :: Cannot Assign Struct Member Values

Feb 13, 2015

I'm a C beginner trying to assign struct member values to other struct members to create a list.

I've tried dot notation, pointer notation, strcpy, memcpy, memmove and normal assignment.

Nothing has worked.

cust_list.h

View 5 Replies View Related

C++ :: Filling Arrays From A Text File

Jun 20, 2013

I am having a trivial trouble on how to create three different arrays from a text file. I am a beginner in C++. I have a .txt file containing a string of 'float' values as below:

0.5
0.6
0.7
0.8
0.9
1.0
1.1
1.2
1.3

//----------------------

Now, I want to make three arrays, p1[], p2[], and p3[] from them, so that
p1[] has elements from line: 1, 4, 7, ...,
p2[] has elements from line: 2, 5, 8, .., and
p3[] has elements from line: 3, 6, 9,... of the .txt file.

My original file has a huge amount of data, so I would have to use a loop, but I cannot think of a way to fill my arrays as described.

View 3 Replies View Related

C++ :: How To Save Two Arrays Into One Text File

May 11, 2013

I have a 4*4 array and a 8*8 arays in my c++ program and i would like to save those arrays in one textfile. How do i do that.

View 5 Replies View Related

C :: How To Assign Values To Pointer Char Array

Aug 10, 2013

I can assign values to pointer character array like this...

Code:

char *array[4]={"abc","xyz","dgf","sdt"} ;

but the case is i don't know how to assign strings through key board ???? with using gets ,getchar or other suitable function

View 2 Replies View Related

C# :: How To Assign Distance Values To Fixed Graph

Apr 28, 2014

I'm working on a Dijkstra's algorithm with a fixed graph. My question is, how should I go about assign distance values to the graph. The user can start from any node.

View 4 Replies View Related

C :: Read Text From A File Into Arrays To Set Categories

May 15, 2013

I'm making a Jeopardy game and when I read text from a file into arrays to set the categories, questions and answers a couple of the strings are not terminating and they are printing that junk box thing at the end.

When I use the debugger and print the problem strings they are showing up as "Thanksgiving21" or "Calvin Klein21" but all the other strings aren't having that problem.

Then for example I will change "line[strlen(line)-1] = '';" to "line[strlen(line)] = '';" and other strings will have that problem but not those stated ones. I'm using line[strlen(line) - 1] = '' to get rid of the newline fgets appends on the end of the strings.

Code:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(int argc, char *argv[]){
FILE *file = fopen("questions", "r");
char *string[66], line[150];
int idx;

[Code]...

View 6 Replies View Related

C/C++ :: How To Assign Text To A Textbox

Apr 24, 2015

I have a silverlight app that uses TextBox XAML controls.

In the c++ code-behind, IXRTextBoxPtr types are associated with these textboxes using "FindName" like this:

FindName(L"ColNum3", &m_pColNum3);

(where ColNum3 corresponds with the XAML CODE like this: )

Then, the code assigns the pointer like this:

std::wstring wsTransfer; // gets the wstring from imput
const WCHAR * wpszInput;
wpszInput = wsTransfer.c_str();
m_pColNum3->SetText(wpszInput);
but the display does not show the text data.

What am I missing? What steps am I missing to have this text modification display on the screen?

View 6 Replies View Related

C++ :: Assign Unique Integer Values For Words In Dictionary

Mar 25, 2013

I need to assign unique integer values to words in a dictionary that have the same alphabets, for example 'act' and 'cat' should have the same integer value. Would just adding the ascii values of the letters be sufficient?

View 1 Replies View Related

C/C++ :: How To Read Information From A Text File And Store It Into Arrays

Mar 28, 2014

how I can read information from a text file into an array so afterwards I can display the array and it will show the contents of the text file?

the information inside my text files consist of names and numbers like so: "Collins,Bills 80" should I separate the numbers and names into two separate text files one for names and one for numbers?

My code so far is this:

#include <iostream> //for cin and cout
#include <fstream> //for input/output files
#include <conio.h> //for getch

[Code]....

View 2 Replies View Related

C/C++ :: How To Assign Lines Of Text Using Combobox

May 5, 2013

So I'm working on program I had in Windows Form Application.

I have a combobox that lists the years 1981-2013. Beside it, a simple Enter button.

I wanted to be able to assign individual lines of text to each year that would be displayed in the message box.

Like if the user selects 1981, clicks Enter and the message box displayed "In the year of 1981.." but if they select 1982, the message box displayed "In the year of 1982.." I have everything else I want working, except that I can't figure out how to assign different lines of text to different years.

View 2 Replies View Related

C++ :: Setting Values Using A Text File?

May 19, 2014

in the following code how would i set a txt file up to where i can have say name = whatever in it and the program will find it and set it to the corresponding global value.

#include <fstream>
#include <iostream>
#include <string>

[Code]....

View 1 Replies View Related

C# :: How To Edit Hex Values Of A Text File

Jan 5, 2015

I have a report that's generated by a piece of software, and (I'm assuming because it was intended for Linux/Unix) when I open to read it in notepad, it has a bunch of funky symbols in it, and incorrect line feed characters instead of returns that windows uses.

I looked at the txt file with a hex editor and can see all the hex codes for the symbols I want deleted. One of the symbols 0A.

How can I do a "find and replace" command but do it with hex codes? These symbols cant be simply placed into my code file because VS does not know how to interpret it.

View 7 Replies View Related

C++ :: Declaring Arrays - Counting Number Of Lines In Text File

Feb 9, 2013

I'm creating a program to read information about class schedules at my school, reformat the information, and allow the user to search for specific semesters. There are eight fields of information. I'm reading the info from a text file using eight parallel arrays, but I'm having trouble declaring the arrays. I can run this code in one compiler (Dev-C++) with no problems, but I get errors when trying to compile it using Visual Studio stating that arrays must be declared with a constant value. I have a loop to run through the text file, with a counter to increment with each subsequent line, then I create a constant int equal to the counter, and declare the arrays of size equal to the constant int. Here's the section of code in question:

// Counting the number of lines in the text file
inFileForLines.open("CIS225HW1DA.txt");
string countLine;
int numberOfLines = 0;
//Discarding the first line of the text file containing only column headings
getline(inFileForLines, countLine);

[Code] .....

View 7 Replies View Related

C++ :: Removing NULL Values In Text File

Sep 2, 2013

I have a measuring board that writes a .txt file in the following format:

board number;type;NUL;channel;measured value;date and time
DS207;5000007;NUL;0;20251;11.07.2013 12:30:02 MESZ
DS207;5000007;NUL;1;10159;11.07.2013 12:30:02 MESZ
DS207;5000007;NUL;4;27.18;11.07.2013 12:30:02 MESZ
DS207;5000007;NUL;0;20233;11.07.2013 12:35:02 MESZ
DS207;5000007;NUL;1;10149;11.07.2013 12:35:02 MESZ
DS207;5000007;NUL;4;27.31;11.07.2013 12:35:02 MESZ
DS207;5000007;NUL;0;20256;11.07.2013 12:40:02 MESZ
...

I would like to extract and analyse the data but the data behind the NUL entry is not accessible for me maybe due to the fact that NUL normally marks the end of a line. Is there a method to remove the NUL entries in this text file?

View 12 Replies View Related

C# :: Looping Through Text File And Recalculating Some Values

Jan 20, 2014

I have chosen C# as our language . Students have to enter a name of a recipe, followed by the number of people it will serve, followed by a list of ingredients for a recipe. This, then, needs to be exported to a text file. This bit I am fine with.

They then have to import the file back into C# and recalculate for a new number of people. This is the bit I am struggling with. The calculation is fine, I can do that. The issue is getting it to loop around each column of data to not only perform a calculation (I have just set it up to multiply by an entered value for now). I have set up a text file with the following data in it:

2 grams sugar
350 ml milk
4 tsp lemon
5 tsp vanilla
6 cloves garlic

I want it to come back with, (if multiplied by 2 for example) something like:

4 grams sugar
700 ml milk
8 tsp lemon
10 tsp vanilla
12 cloves garlic

At present, it comes back with

4 grams sugar
4 grams sugar
4 grams sugar
4 grams sugar
4 grams sugar

Code is below.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
namespace ConsoleApplication1{

[Code] .....

View 3 Replies View Related

C++ :: Passing Values From Text File To Integer Array

May 24, 2013

I'm trying to passing values from a text file to an array. The values in txt file is;

0 2 3 5
1 2 5
0 1 3 6
1 3 5

I need to store these values in an array like that

Code:
int array[?][?]={{0,2,3,5},{1,2,5},{0,1,3,6},{1,3,5}};

View 1 Replies View Related

C :: Search Table And Replace Values In Text File

Feb 3, 2015

I have to develop a C program that reads a very complex txt file (output of a calculation code, so full of mixed text and numerical tables), search for a specific table in it and individuate/print or replace a specific value with assigned position in the table.

View 2 Replies View Related







Copyrights 2005-15 www.BigResource.com, All rights reserved