Hi, this code is merely for testing a coulpe of functions for use in a bigger program, therefore this code alone may seam silly, but there is a point to it:)

When i run this and press "1" the program just terminates, pressing "2" or "whatever here" works fine however? so, what am i doing wrong guys?

#include <iostream>
#include <fstream>

using namespace std;

void skrivtilfil (string placering);
void laesfrafil ();
int main()
{
    int ioro;
    cout << "in or out? ";
    cin >> ioro;
    if (ioro == 1)
    {
    string plac;
    getline (cin,plac);
    skrivtilfil (plac);
    }
    else if (ioro == 2)
    {
    laesfrafil ();
    }

    return 0;
}

void skrivtilfil (string placering)
{
    ofstream skrivtraed;
    skrivtraed.open ("Seek and Hide DB.ini",ios::app);
    skrivtraed << placering, "\n";
    skrivtraed.close ();
}
void laesfrafil ()
{
    string line;
    ifstream laestraed;
    laestraed.open ("Seek and Hide DB.ini");
    while (! laestraed.eof() )
    {
        getline (laestraed,line);
        cout << line;
    }
    laestraed.close();
}

Could you maybe explain us first what the goal of the program you posted is? (I mean: what output do you expect, etc.)

To answer the question in your signature:

Yo god, where do i report a bug?

Start a new thread about the bug in the DaniWeb Community Feedback forum (check first whether the bug you're going to report isn't already reported!).

To finish this post:

Help me fail searching (if statement - ****** up)

Sorry, but I'm unable to see the 'link' between this and your code, could you maybe explain this, or choose a more appropriate thread title in future?

Don't put this dirty words in your title, put better titles and that explain what you need!

Thanks!

commented: I did explain my needs, i even did so in the title thank you, please read before making angry posts +0
commented: Perfectly reasonable post. There is no need to swear. +24

This seems to work fine for me...

#include <iostream>
#include <fstream>

using namespace std;

void skrivtilfil (string placering);
void laesfrafil ();
int main()
{
	int ioro;
	cout << "in or out? ";
	cin >> ioro;
	if (ioro == 1)
	{
		std::cout << "Pressed 1. " << std::endl;
	}
	else if (ioro == 2)
	{
		std::cout << "Pressed 2. " << std::endl;
	}

	return 0;
}

Daviddoria: thats exactly what I dont get:/
Could someone please try and compile the code and check if it works on your system?

Well, im sorry if I offended anyone, that was really not my intention, I guess im more 'large' about english swear words as it is not my native tounge.

Anyway, I think the title describes my problem quite well actually?
i need help with finding the mistake I've made.
And as the problem occurs around the if statement, it is exactly an if statement fuck up, so please dont give me that 'bad title' stuff, but again, im sorry if I offended anyone.
I just wish to find out what I am doing wrong and then fix it.

Tux4life: well, the goal of this program is simply to accept and display input. (through a file)
I expect each input to be written in separate lines in the .ini file, and I expect one line to be displayed at the time.
It is a piece, meant to be part of a bigger program.
But the if statement is going the way I wanted it to:)
(I think it is the if statement)

To the OP:
No offense, but I can't trust the code you posted down.
Wait, let me explain, I came across a line which looked quite strange to me, and which will never let me compile this program: skrivtraed << placering[B],[/B] "\n"; .
Could you otherwise just copy and paste your code directly from your text editor?
(Try to compile it first, only when it compiles, you copy and paste it again, otherwise you post down the error messages together with the not-compiling code).

As it seems like you're able to compile that:
Are you sure that the file Seek and Hide DB.ini exists in the same directory where you run your program from (at the moment when you choose option 1)?
Note that you're opening the file in appending mode, which means that the file has to exist already before you can add any other stuff to it.

Also, you don't seem to check whether the file indeed could be opened, what if you try to read/write from a file which couldn't be opened?
You can easily add a check, like I'll demonstrate in the following example:

ofstream fout("test.txt");

// Check whether the file could be opened succesfully
if( !fout.is_open() )
{
   // The file couldn't be opened
   // Put your code to handle such a situation here
}

When using a check like shown above, then (and I can never say it enough times) make sure that you don't read/write from/to the file in the case that it couldn't be opened.

Another really annoying thing is: using eof() as to control a loop, like you did here:

void laesfrafil()
{
    string line;
    ifstream laestraed;
    laestraed.open("Seek and Hide DB.ini");
    
    while ( !laestraed.eof() ) // bad coding style
    {
        getline(laestraed, line);
        cout << line;
    }
    
    laestraed.close();
}

Fixing that issue is easy:

void laesfrafil()
{
    string line;
    ifstream laestraed;
    laestraed.open("Seek and Hide DB.ini");
    
    while ( getline(laestraed,line) ) // notice the change here
    {
        cout << line;
    }
    
    laestraed.close();
}
commented: It's all good :) +36

Tux4life: im sorry, what did you want me to do with the text editor?

And i understand you concern about compileing the code, but obviously it makes a file in the directory you run the .exe in, and in that file it writes whatever you type in, it should be quite safe, i mean you can actually see the 'source code' so how dangerous can it be:)

Tux4life: im sorry, what did you want me to do with the text editor?

Well, you know the text editor? I meant: the program where you write your code in, launch that program, open the file containing the code of your program, copy that code, and paste it in a new post in this thread :).

I allready did that:) in the first post:)
i havent written the complete program yet.
But when i run the program and press 1, it doesent even wait for input.
it seems like the getline (cin,plac); is ignored?

it doesent even wait for input.
it seems like the getline (cin,plac); is ignored?

Yes, it doesn't wait for input. It isn't ignored, but it DOES ignore YOU, as in it doesn't wait for you to enter anything because it already has an unused newline in the cin stream left over from the earlier >> operator input. It's a common enough problem when you read input using >>, then attempt to read more with a get or a getline commend. You need to flush the input stream. How to do it is explained here

Second, see tux4life's earlier post regarding this line:

skrivtraed << placering , "\n";

It actually DOES compile, but I doubt it's what you want. You're using the comma operator and I imagine you want to use the << operator instead.

commented: VERY usefull +1

Which IDE are you using? (Visual studio, KDevelop, etc) I know in KDevelop the little command window is broken such that it just ignores cin type statements... seems very odd but I always have to open a real terminal window and run the program if my program expects input.

I use code::blocks, and im quite sure i've used cin before:)

About the comma contra <<, im sure you are right, but what is the difference actually?
Oh, and im gonna look into that flushing the input stream thingy, seems rather complicated though, is there another way around it? like getting a whole line whitout getline? i think i read about some parameters for cin once that allows the 'programmer' to choose what character should end the string?

EDIT: Oh! and i almost forgot: tux4life, why is your sollution better than using the loop with eof, nad how does it work?
not that i doubt you, but i would like to know why (you know, some "dont give me a fish, teach me how to catch one" sort of thing:-) )

About the comma contra <<, im sure you are right, but what is the difference actually?

You have to solve the getline/ignore problem first before you'll notice any difference regarding << and the comma operator. Suffice it to say, if you didn't intentionally use the comma operator for some good reason (and I see no reason to use it here), use the << operator. The comma operator is rarely used by most people. I don't even remember how to use it or when to use it really.

Oh, and im gonna look into that flushing the input stream thingy, seems rather complicated though, is there another way around it? like getting a whole line whitout getline? i think i read about some parameters for cin once that allows the 'programmer' to choose what character should end the string?

You can use any delimiter you want with getline. The default is '\n' if you don't specify one.

http://www.cplusplus.com/reference/string/getline/

getline is the easiest way to get a whole line. If you don't want to use ignore, have two getlines, one of which is just to eat up what >> leaves over. Add lines 4 and 5.

int ioro;
    cout << "in or out? ";
    cin >> ioro;
    string restOfLine;
    getline (cin, restOfLine);

Then go on with the rest of the code and have a getline that reads in data you actually care about (restOfLine will just be an empty string probably).

Thanks this is great!
i'll still look in to flushing the input stream, as it seems like a good thing to know about.

If you'll allow it, i have a questions that is kind of of topic:
Can i make the program run again from the top without putting all of into a loop?
I've been told goto is a BAD idea, and i dont like the thought of just making the program run the program again at the end, so is there a way to run the 'start menu' again after executing a function?

Hmm, maybe i should make the menu a function?
ad then do something like this:
(written in pseudo code, obviously:) )

void menu ();
{
if (userinput == 1)
{
write to file
}
else if (userinput ==2)
{
read from file
}
else //or nothing here?
}

Thanks this is great!
i'll still look in to flushing the input stream, as it seems like a good thing to know about.

If you'll allow it, i have a questions that is kind of of topic:
Can i make the program run again from the top without putting all of into a loop?
I've been told goto is a BAD idea, and i dont like the thought of just making the program run the program again at the end, so is there a way to run the 'start menu' again after executing a function?

Hmm, maybe i should make the menu a function?
ad then do something like this:
(written in pseudo code, obviously:) )

void menu ();
{
if (userinput == 1)
{
write to file
}
else if (userinput ==2)
{
read from file
}
else //or nothing here?
}

"Goto is always bad" is overly-simplistic. If "goto" was always bad, I imagine it would be removed from the language. It hasn't been. Hence there must be at least some times when it must be good. I've never really used goto. I think it may be appropriate for certain nested loops where you want it to go to a certain spot and "break" and "continue" won't work or whatever. Googling "c++ goto bad" yields some interesting links. A while loop seems more appropriate for your needs. It sounds like you don't want to use one. Why?

I just dont like the idea of packing my whole program into a loop, but i guess thats sort of ridiculous :)

But howw ould that while loop look?
I mean, what should dertimine weather the loop should, well loop, or terminate?

I just dont like the idea of packing my whole program into a loop, but i guess thats sort of ridiculous :)

But howw ould that while loop look?
I mean, what should dertimine weather the loop should, well loop, or terminate?

Try using something like this as a skeleton. Change to your needs.

#include <iostream>
using namespace std;


int Menu ()
{
    cout << "1. Add" << endl;
    cout << "2. Subtract" << endl;
    cout << "3. Multiply" << endl;
    cout << "4. Quit" << endl;
    cout << "Enter a number : ";
    int choice;
    cin >> choice;
    return choice;
}

void GetTwoIntegers (int& num1, int& num2)
{
    cout << "Enter first number : ";
    cin  >> num1;
    cout << "Enter second number : ";
    cin  >> num2;
}

void Add ()
{
     int num1, num2;
     GetTwoIntegers (num1, num2);
     cout << num1 << " + " << num2 << " = " << num1 + num2 << ".\n";
}

void Subtract ()
{
     int num1, num2;
     GetTwoIntegers (num1, num2);
     cout << num1 << " - " << num2 << " = " << num1 - num2 << ".\n";
}

void Multiply ()
{
     int num1, num2;
     GetTwoIntegers (num1, num2);
     cout << num1 << " * " << num2 << " = " << num1 * num2 << ".\n";
}

int main ()
{
    int choice;
    
    do 
    {
        choice = Menu ();
        switch (choice)
        {
            case 1: Add (); break;
            case 2: Subtract (); break;
            case 3: Multiply (); break;
        }
    }while (choice != 4);
    
    return 0;    
}

will do, thanks for you help everyone, gonna read the topic through and make some possitive ratings:)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.