if/else statement

I'm writing a program that is designed to respond to user input. I have one problem at the moment. i need to write a statement that will check the value of a string variable. it's supposed to to the following:

if the variable is equal to "" then it will run a save data function(which i've got sorted.) i'm using dev C++

has anyone got any ideas ?
1
2
3
4
5
6
std::string variable;
//...
if (variable == "")
    savedata();
else
   doNothing();
Thanks.
Do you possibly know the syntax for a function that accepts variables that i declare outside of it as arguments?
Do you just need to pass a copy?
void foo(int x) //Takes integer argument called 'x'
If you need to change it inside:
1
2
3
4
5
6
7
8
void bar(int & x) //Takes refetence to int x
{
    x+=10;//x increased by 10 here
}
//...
int a = 5;
bar(a);
std::cout << a;//Prints 15 
I need the function to accept string variables as arguments.
void foo(intstring x)//Voila
Anything else?
Thanks, one more thing.(sorry!)
what is wrong with my code. i keep getting errors like my function has already been defined or something related to my seperate source file which is linked to main.cpp.

below is main.cpp:

// Talk to the Computer V 1.0.0 - this program is designed to respond
// to input from the user.
// main.cpp
// Header files
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include "functions.cpp"
using namespace std;

// Main Program
int main(int nNumberofArgs, char* pszArgs[])
{
string Name;
if(Name == "")
{
cout << "Hello there.\nHave we met?" << endl;
cout << "What's your name?" << endl;
cin >> Name;
Savedata(Name);
cout << "The value of the variable called"
<< "is " << Name << ".\n";
}

// wait until user is ready before terminating program
// to allo user to seee the program results
system("PAUSE");
return 0;
}


and below is functions.cpp (where i store my functions):

// Functons for main.cpp
// functions.cpp
// Standard Template Library Files only
#include <fstream>
#include <string>
using namespace std;

// Savedata Function
void Savedata(string args)
{
ifstream TakeData;
ofstream SaveData;
TakeData.open("data.dat");
TakeData << args;
TakeData.close();
SaveData.open("data.dat");
SaveData >> args;
Savedata.close();
}
1
2
3
TakeData << args; //Should be >>
SaveData >> args; //Should be <<
Savedata.close(); //Should be SaveData 
Thanks alot, but now i have one problem left. (really really sorry !)
my compiler is saying:

multiple definition of `Savedata(std::string)'
and it is also saying:

first defined here

and

[Build Error] ["Talk] Error 1
the compiler is saying from first def..... .......[Build Error]

that the problem is in main.cpp.
That means exactlys that: function is defined twice.
I suppose that you have included functions.cpp in project compile list, so there will be 2 SaveData() functions in program: one in compiled functions.cpp and one in main (you included functions.cpp here)
I haven't got any other files called functions.cpp and the Savedata() function hasn't been refernced anywhere else in the program
I have even deleted all the compiled versions of all the files.
So it means that function.cpp in your build list. either delete it from there or create a header for your cpp file and include that in main.
How your code looks after preprocessing:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
/*main.cpp*/
// Talk to the Computer V 1.0.0 - this program is designed t...
// to input from the user.
// main.cpp
// Header files
#include <cstdio>  //<\
#include <cstdlib> //<--I didn't expand these because they're HUGE
#include <iostream>//</
// Functons for main.cpp
// functions.cpp
// Standard Template Library Files only
#include <fstream>
#include <string>
using namespace std;

// Savedata Function
void Savedata(string args) //<-First definition
{
ifstream TakeData;
ofstream SaveData;
TakeData.open("data.dat");
TakeData << args;
TakeData.close();
SaveData.open("data.dat");
SaveData >> args;
Savedata.close();
} 
using namespace std;

// Main Program
int main(int nNumberofArgs, char* pszArgs[])
{
string Name;
if(Name == "")
{
cout << "Hello there.\nHave we met?" << endl;
cout << "What's your name?" << endl;
cin >> Name;
Savedata(Name);
cout << "The value of the variable called"
<< "is " << Name << ".\n";
}

// wait until user is ready before terminating program
// to allo user to seee the program results
system("PAUSE");
return 0;
}
/*function.cpp*/
// Functons for main.cpp
// functions.cpp
// Standard Template Library Files only
#include <fstream>
#include <string>
using namespace std;

// Savedata Function
void Savedata(string args) //<-Second definition
{
ifstream TakeData;
ofstream SaveData;
TakeData.open("data.dat");
TakeData << args;
TakeData.close();
SaveData.open("data.dat");
SaveData >> args;
Savedata.close();
} 
Last edited on
is this all working code?
if so shall i delete functions.cpp (the source file)?
It is there to show where tjhe problem is.
Methods of solving it here:
So it means that function.cpp in your build list. either delete it from there or create a header for your cpp file and include that in main.
I've just put the function into main.cpp and now it all works except the if statement. instead of continuing with the rest of the program it skips to:

system("PAUSE");
Topic archived. No new replies allowed.