GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 08-Mar-2004, 20:25
jerry jerry is offline
New Member
 
Join Date: Mar 2004
Posts: 18
jerry is on a distinguished road

regarding main method


hi im new to the whole File I/O thing so is it possible if i do not include the File I/O codes in the main method?
such as i have another method which i did separately from the main method. I'll only invoke this method when i call it in the main. does it work? i mean will it work?
example:

CPP / C++ / C Code:
int main()
{
singleShots();
return 0;
}

int singleShots()
{
//my codes
}
  #2  
Old 08-Mar-2004, 20:29
dsmith's Avatar
dsmith dsmith is offline
Senior Member
 
Join Date: Jan 2004
Location: Utah, USA
Posts: 1,351
dsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of light
Hello Jerry.

I am not sure what you mean. So far your code is 100% valid as long as you declare you function singleShots as follows:

CPP / C++ / C Code:
int singleShots();

int main()
{
singleShots();
return 0;
}

int singleShots()
{
//my codes
}

You can avoid this declaration if you put singleShots above your main function. What is "my codes" going to do?
  #3  
Old 08-Mar-2004, 20:40
jerry jerry is offline
New Member
 
Join Date: Mar 2004
Posts: 18
jerry is on a distinguished road
oh i guess it means it should b ok...coz i thought i need a main method for the File I/O codes...
oh yes i have a couple of errors:

CPP / C++ / C Code:
int single_shot(int a,int b)
	{
		std::ofstream file("measurements.txt", ios::app);
		file << "SINGLE\n";
		send_command(a,MSG_SINGLE_SHOT,0);
		if (!get_response(b,DATA_PACKET))
		return(FALSE);

		file << parse_data(&response) << "\n";
		
		file.close();
		return(TRUE);
	}

errors:
error C2143: syntax error : missing ';' before ':'
error C2065: 'file' : undeclared identifier
error C2297: '<<' : illegal, right operand has type 'char [8]'
error C2297: '<<' : illegal, right operand has type 'char [2]'
error C2224: left of '.close' must have struct/union type

when i added the 'std::' the syntax error appeared after i compiled...i chked all the syntax there's nothing wrong ??:
  #4  
Old 08-Mar-2004, 20:47
dsmith's Avatar
dsmith dsmith is offline
Senior Member
 
Join Date: Jan 2004
Location: Utah, USA
Posts: 1,351
dsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of light
Is this at the top of your file?
CPP / C++ / C Code:
#include <iostream>
#include <fstream>

Also, I have found in C++ files that if you include
CPP / C++ / C Code:
using namespace std;
near the top of the file you shouldn't have to use the std:: before the types.

Quite frankly, I am not a great c++ programmer. I mostly use C style programming. If you are still gettin errors, why don't you post the entire thing. If it is long, you can post it as an attachment.
  #5  
Old 08-Mar-2004, 20:58
jerry jerry is offline
New Member
 
Join Date: Mar 2004
Posts: 18
jerry is on a distinguished road
i included both files

CPP / C++ / C Code:
#include <iostream.h>
#include <fstream.h>

if i included the namespace thing, the compiler cant identify it. i dont know why. it will give me an error: undeclared identifier. yeah my codes are long. mayb i should just attach it n you take a look...it's ok if you cant solve it too...it's such a headache!
Attached Files
File Type: zip codes.zip (9.2 KB, 15 views)
  #6  
Old 08-Mar-2004, 21:28
dsmith's Avatar
dsmith dsmith is offline
Senior Member
 
Join Date: Jan 2004
Location: Utah, USA
Posts: 1,351
dsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of light
Jerry, what compiler are you using? This is a basic C file (it has a .c extension and most of it seems to be standard c). Is there a reason that you are adding the single_shot function using c++ syntax?

The gcc compiler for example will compile the program based upon the extension (.c is c, .cpp is c++). Basically if this is getting compiled as a c file, what you have added will not work.
  #7  
Old 08-Mar-2004, 21:30
jerry jerry is offline
New Member
 
Join Date: Mar 2004
Posts: 18
jerry is on a distinguished road
really??? oh gosh....ok...so this means i should change all the codes into c? or just change the compiler? oh my....i've been spending time on it like....for so many weeks!
  #8  
Old 08-Mar-2004, 21:31
WaltP's Avatar
WaltP WaltP is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Midwest US
Posts: 3,243
WaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to all
dsmith suggested
CPP / C++ / C Code:
#include <iostream>
#include <fstream>

You included:

CPP / C++ / C Code:
#include <iostream.h>
#include <fstream.h>

Do you notice a difference? Try dsmith's suggestion again.
  #9  
Old 08-Mar-2004, 21:34
dsmith's Avatar
dsmith dsmith is offline
Senior Member
 
Join Date: Jan 2004
Location: Utah, USA
Posts: 1,351
dsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of light
Quote:
Originally Posted by jerry
really??? oh gosh....ok...so this means i should change all the codes into c? or just change the compiler? oh my....i've been spending time on it like....for so many weeks!

Jerry, if it were me I would code it in C syntax to begin with.

Anyway, what compiler are you using. Are you telling it to compile as a c++ program? A c program can be compiled as a c++ program, but not vice a versa. Also, just for semantics you should change the extension to .cpp
  #10  
Old 08-Mar-2004, 22:23
aaroncohn's Avatar
aaroncohn aaroncohn is offline
Regular Member
 
Join Date: Feb 2004
Location: Bay Area, CA.
Posts: 564
aaroncohn is a jewel in the roughaaroncohn is a jewel in the roughaaroncohn is a jewel in the rough
Didn't you post something exactly like this under a different name a week ago, jerry?
 
 

Recent GIDBlogObservations of Iraq by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Method POST is not allowed on the URL jwalsh Computer Software Forum - Windows 2 27-Feb-2004 15:36
Can main host co-exist w/virtual host? gongkuan Apache Web Server Forum 1 30-Nov-2003 02:14
Best method to get listed on Google no1special_1999 Search Engine Optimization Forum 4 23-Sep-2003 23:34
Main page gone down to PR4 jrobbio GIDTopsites™ 9 04-Feb-2003 01:29

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 04:57.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.