C++ :: Proper Use Of Seekg

Apr 28, 2013

I am trying to change the position stream pointer for input to the beginning of a file using

Code: encodedfile.clear();
encodedfile.seekg(0);

but after printing out encodedfile.tellg() afterwards, the pointer stays at -1 and I can't read any of the text inside the file.

Here is the declaration of the variable

Code: std::fstream encodedfile(encodedfilename,std::fstream::in | std::fstream::out); and this is where text is outputted to the file Code: while(file >> std::noskipws >> ch)
{
encodedfile << hasbinary[(unsigned int)ch].binaryencoding;
cout << hasbinary[(unsigned int)ch].binaryencoding;
}

Is there another way to return the stream pointer to the beginning or is there a way to fix my code?

View 2 Replies


ADVERTISEMENT

C/C++ :: Switch Statement Using Seekg

Mar 21, 2014

Right now I have to use seekg to read a letter from a text document then using a switch statement i have to show what that letter is

#include <iostream>
#include <fstream>
#include <math.h>
#include <time.h>

[Code]....

View 7 Replies View Related

C++ :: The Proper Way To Do A Vector Of Objects

Oct 20, 2014

I've been really busy but managed to get in enough down time to learn somewhat decent info about vectors. Anyways originally my program created a dynamic array of pointers to class objects and I came across a few problems because of this. Apparently an array of pointers is now outta of the question and I will now be switching to a vector of objects instead.

Why I want a list of objects instead of pointers this little comment should clear things up.

tiles[i]->show() dereferences tiles[i] (i.e. accesses whatever it points at) before calling the show() method.

That is undefined behaviour. Once undefined behaviour occurs, there is no recovery, and there is nothing the show() method (or any subsequently called function for that matter) can do to recover (short of invoking their own forms of undefined behaviour - compiler specific hacks, etc).

Even if the show() method initialises the object correctly, it cannot change the pointer tiles[i] which is in a different scope (within main()).

What I'm trying to do is create a vector of already intialized objects so that I can use a conditional statement of every single element to properly layer my games art resources. This should also automatically fix a mild unrelated collision dectection problem too but first thing first layering.

View 9 Replies View Related

C :: Can't Show The Proper Float Value

Jul 7, 2014

I am working from my "ansi c" book by steven lawlor, page 73 program 2. write a program that accepts two numbers from the keyboard and prints the following information.

variables
first
second
execution
First number ? 7
Second number ? 2
the second goes into the first 3 times
with a remainder of 1.
the quotient is 3.5.

Code:
#include <stdio.h>
main() {
int first, second;
scanf(" %1i %1i", &first, &second);
printf("First number ? %1i

[Code] ....

//I included this as I had some error message come up
// before, not sure if this is correct though?
} it shows what is expected but I cant get the 3.5.

I have tried %f and variations of width/precision but still not luck. Also, when I click on the application and put in the variables I press enter, the program executes and disappears so I cant see the result. how do I get the program to stay up until I want to get rid of it?

View 1 Replies View Related

C :: Program Come Out Without Proper Execution

Jul 11, 2013

the following code doesn't execute properly( After the second entry the program comes out)!!

Code:

#include<stdio.h>
void linkfloat();
int main(){
struct book {
char name;
float price;
int page;
};

[code]....

View 8 Replies View Related

C :: Displaying Proper Value With Arrays?

Jun 30, 2014

I'm currently working on a temperature conversion program using arrays / pointers as practice.

I will post the code below, its quite a bit lengthy and its also incomplete. Everything was going smoothly and I continually test my code as I write to completion, then I ran into a small road block.

My problem here is regarding the output from the first if statement in main. When I enter a value to convert from F to C. It successfully converts the first value I enter, but any other value after the first one during the loop in the first if statement, just shows long numbers of all sorts.

The output looks like this:

I kept checking the logic behind what I did, and for me it seems to be correct. Here is the code:

Code:

#include <stdio.h>
#define array_size 5
#define NEWLINE printf("
")

[Code].....

View 4 Replies View Related

C++ :: Proper Way To Read TXT File?

Nov 1, 2013

I want to know the best way to read files.

ifstream infile;
int x;
char y, *yy;
string z;

For each of the 4 variable types, what is the best way to read the form the file? Also, if I want a delimiter, how would I use that?

Currently I have:
getline(infile, z, ".");

That wont work. (I want it to collect everything into the string, up until the full stop. Also what are the specifics, any approaches listed.

e.g. infile >> x;collects first integer, does it leave whitespace to be read by the next thing? Or does it remove the whitespace from the buffer? Because if it does, what if I wanted something like say:

infile >> x;
infile >> z;
Where there .txt file reads: 15:test'.
Would z = :test'. or test'.

View 2 Replies View Related

C++ :: Proper Way To Define A Variable Type

Sep 17, 2013

I just compiled some code I've been working on at a different OS/compiler and realised that Code: sizeof(unsigned long) returns 4 in one pc and 8 in another.

I've heard that bytesize conventions for basic variables were not particularly "universal" before but this is the 1st time I've had a problem with it.

how do I make a typedef that clearly indicates to whatever compiler compiler I want u32 to be an 32bits unsigned and u64 to be 64bits?

View 12 Replies View Related

C :: Create A Proper Table That Accepts Value In Each Box It Contains

Sep 29, 2013

I want to create a proper visible table with boundaries that contains boxes and each box receives a value .I don't know where to start from.i have an idea of using matrix for entering values in each box of table,but how to create lines and boundaries ?

View 1 Replies View Related

C++ :: Proper Fundamental Type Overloading

Feb 5, 2014

I am currently working with the fundamental types in C++, and there is some ambiguity (which I have tried to solve).

I am creating a function that needs an overload for _all_ fundamental types (in addition to a void *).

Some types overlap (like "unsigned wchar_t" and unsigned int, whilst wchar_t never overlaps), so, I would like some confirmation about my current overloads being complete and overlap-free on your system as well.

bool
char
unsigned char
signed char

wchar_t
char16_t // In fact, builtin-types, look like typedef.
char32_t

[Code] ....

To test the valid types, I have created a small test that shows which builtin types overlap and which do not.

#include <map>
#include <vector>
#include <iostream>
#include <string>
#include <typeinfo>
int main(int argc, char *argv[]) {

[Code] ....

I also wonder; what "type" is nullptr? I tried googling but no definitive answers came up. Still, it works as a valid function overload.

View 3 Replies View Related

C++ :: Proper Declaration Of Dynamic Arrays

Dec 10, 2013

I started to practice some C++. I use to program in C and a little C++. Anyway, I am writing code that creates a dynamic array. I would like to be able to do something like

galaxyobject[object] -> uniqueid = in the class but I do not think I have it setup right.

Question.
1. Is the code correct?
2. Why can't I use the above line without a compile error or segment fault?

int main() {
cout << "ProteusCore Server" << endl;

// create a galaxy system with a certain amount of objects
galaxy galaxysystem;
galaxysystem.IntializeGalaxy(100);

[Code] ....

I made the code available to see on sourceforge as Proteus 3d Game Engine. It is something I would like to work on.

View 3 Replies View Related

C# :: Proper Validation Of Text Boxes?

Dec 1, 2014

So I am writing a multiform multiclass project for a class and I initially set out to create a validate class to check all of my text boxes. However after writing a few simple boolean functions to check if the box was empty or not, or if it was indeed an integer, decimal ect. I realized I was going to have to call these functions for every text box and also have all of my decision making based on the returned boolean variable still in the code portion of my form. I was wondering how do most people do it, should I write a class that validates the entire form at once(one form has 7 text boxes, one has 9). I suppose I am asking what would the industry standard be, how would a professional do it? While my simple functions would work, and while I could write one large function that validates them all at once both paths leave something to be desired in terms of re-usability for the entire form validation, and compact/cleanliness of code for the simple functions.

My validate class currently stands like this:

class Validate {
bool isValid = true;
int trash;
bool valid_string(string input) //parameter is textbox.text property {
if (input == string.Empty)

[Code] ......

View 7 Replies View Related

C++ :: Proper Way Of Using String Array Inside A Class

Jul 23, 2014

class myclass{
string myarray[5];
myarray[0] = "one"; myarray[1] = "two"; myarray[2] = "three"; myarray[3] = "four";
myarray[4] = "five";
};

this works in main function but doesn't work in the class;

1. Why this methods doesn't work inside class but works in main function?
2. What is the proper way of using string array inside a class?

View 4 Replies View Related

C# :: Output Two Dimensional Array With Proper Formatting

Jan 23, 2014

I have a 3x3 array which is filled up of random numbers, the limit being set by the user. So if the user chooses 3, the array will be filled up of 1s, 2s and 3s randomly. That bit is fine.

So when I display the array I use:

Console.WriteLine("The values in the 2 dimensional array are: ");
foreach (int number in twoDimensional)
Console.Write(number.ToString() + " ");

The output will then be 3 3 2 1 3 1 2 3 3. Is there a way I can display it like:

3 3 2
1 3 1
2 3 3?

View 4 Replies View Related

C++ :: Template Class Instance - Proper Datatype

Oct 20, 2012

I have a class that is a template, I have to declare it in my main but i want the user to choose what type of data they will use in the class, I cant just declare myclass my, i have to use myclass<int> my, how can I change so user can select the proper datatype to use in the class.

View 5 Replies View Related

C :: Program Won't Correctly Print Proper Totals On Screen

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

C++ :: Compiler Not Having A Proper Order Of Compilation For Header Files

Jun 1, 2014

Today I experienced a very strange compiler issue. I started the compilation and it outputted that a member object of a class was undefined. After about 4 hours of trying the find the bug I commented and then uncommented said line of code that was undefined. Sure enough the compilation worked just from commenting and uncommenting.

I am using Microsoft visual studio 2012 express. Due to the size of the project, I should know the cause because it may cause more problems further down the line. I feel that it might have something to do with the compiler not having a proper order of compilation for the header files and that I might need something to solidify the way that the header files are processed. The below code is a fragment of a header file.

class CInGameSection: public CBaseGameSection {
public:
CInGameSection(bool isInEditor, bool creatingNew, COutput& output, string saveSlot, string regionName, horiVertiVals * widthAndHeight);
~CInGameSection();
bool update(CInput & input, CAudio & audio, COutput & output);
void output(CInput & input, CAudio & audio, COutput & output);

[code]...

View 3 Replies View Related

Visual C++ :: MFC Proper Usage Of Sending Message To Dialog To Activate

Jan 7, 2015

I have finally got around to developing C++ & MFC using Visual Studio 2012, to build a full GUI Windows application (not sure if I have made the right choice). Though admittedly in two months time I do have a dialog window at my beck and call.

Now I have discovered a flaw (suspect) partially caused by the design of this program not all following the same principles, nor written by the same people and long predates me. Let describe the situation.

I have a dialog I have created as a class which has a combo box. The list of items are populate during the OnInitDialog() function just fine, except if the file where the detail is not yet read from that combo box would not have any items other than the default. This is to be expected in the use of the program, so fine.

However, if the dialog (modeless) was already open and active, when the user went up to and selected the menu command (main window) to read that file that CPtrList data structure that held that data would not populate that Combo box on my dialog.

So what I would like when I am done reading the files contents is to detect if my dialog is opened and if so send it a message to Activate (force a call to OnActivate()). I have gone to the event list for the dialog and exposed this event handler from the resource editor just fine.

I see the syntax of OnActivate() is

void MyClass::OnActivate(UINT nState, CWnd* pWndOther, BOOL bMinimalized);

So nState is an Unsigned Int of the current state of the thread/window (not sure)?

CWnd* pWndOther is the CWnd of my other Window that wants my dialog to activate I think which is the main application since it was on the a menu that this function was called to read the file.

bMinimalized is whether my dialog is mimalized(?)

View 10 Replies View Related

C++ :: Convert Passed String Into Proper Alpha Case And Sort Array Of Names

Apr 25, 2013

I want to create 2 functions to (1) convert a passed string into proper case (capitalize the first alpha character of a string and every alpha character that follows a non-alpha character); (2) sort the array of names (after it has been converted to proper case).

I tried these two:

console.writeline (lower.toUpper());
void bubblesort (string array[],int length);

but I don't know if they are good.

View 2 Replies View Related

C++ :: Display Fraction In Proper From Based On 2 Arguments Passed To Class Member Function

Mar 15, 2015

We're assigned a project working with classes and fractions. My goal is to display a fraction in proper from based on 2 arguments passed to a class member function proper();

My strategy was to utilize the greatest common factor between the 2 arguements, then divide both the numerator and denominator by that number and then it would display.

The program actually runs, but only seems to divide the numerator and not the denominator. This in return makes my other class member functions have incorrect comparisons and sums.

Code:
#include<iostream>
#include<conio.h>
class Fraction {
friend void compare(Fraction a, Fraction b);
friend void sum(Fraction a, Fraction b);

[Code] ....

View 14 Replies View Related

C++ :: Proper Way To Get Info Of One Class Into Another Class

Jan 27, 2015

I am trying to setup my game. first I have my main.cpp which just creates

Game game;
game.run();

I also have my Player class but having trouble accessing the player information from the game class. how can I make it so that my game class has access to player class? or any other class for that matter as my game class will be running everything.

View 3 Replies View Related

C# :: Proper Way For Class B To Use Class A?

Nov 18, 2014

Let's say that I have Class A. I then have Class B that is different. It is now time to add to Class B and I want to use a lot of the methods that are contained in Class A.

How should I go about tying Class A to Class B? I'm not thinking about Inheritance because Class B is not really an extension of Class A. But for the future, if myself or anyone else wants to use Class B, how would it make it apparent that Class A must be included other than the fact that I will see compiler errors all over?

View 7 Replies View Related







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