473,394 Members | 1,674 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

simple output input ....

Hi
Ok...I got a problem in reading a file containning information, and
trying to send it to a function...
the file containing information has format:
Gas 10000 Home Gas Meter
Electricity 3000 Home Electricity Meter
Flight 8000 Holiday Abroad
Flight 1000 Long Weekend
Car 1250 Own Car (10000 miles)
Car 500 Holiday Hire Car

I would like to put the first column to be stored in an array called -
char fuel [ ]
second column - double amount
third column - char description [ ]

the first and second will be used by a fuction called double co2(char
fuel [ ], double amount)
and the third will be stored in the int main ....

Second Question...Can you return 2 values from a function double
co2(char fuel [ ], double amount)??

Thanks
Chris

May 13 '07 #1
11 1633

<ch****@gmail.comwrote in message
news:11**********************@u30g2000hsc.googlegr oups.com...
Hi
Ok...I got a problem in reading a file containning information, and
trying to send it to a function...
the file containing information has format:
Gas 10000 Home Gas Meter
Electricity 3000 Home Electricity Meter
Flight 8000 Holiday Abroad
Flight 1000 Long Weekend
Car 1250 Own Car (10000 miles)
Car 500 Holiday Hire Car

I would like to put the first column to be stored in an array called -
char fuel [ ]
second column - double amount
third column - char description [ ]

the first and second will be used by a fuction called double co2(char
fuel [ ], double amount)
and the third will be stored in the int main ....

Second Question...Can you return 2 values from a function double
co2(char fuel [ ], double amount)??
Call fgets() to read the input, one line at a time.
Once you've got the line into a temporary buffer, you need to break it up.
This is actually quite fiddly, but not conceptually difficult.
You can get the first word by reading until you hit a space character, the
number by calling strtol(), and then the end pointer that you pass to strtol
gives you the remainder of the line. However you should also deal with any
unexpected inputs.
Another way is to use fscanf(). However this is very easy to use unsafely.
--
Free games and programming goodies.
http://www.personal.leeds.ac.uk/~bgy1mm

May 13 '07 #2
ch****@gmail.com wrote:
Hi
Ok...I got a problem in reading a file containning information, and
trying to send it to a function...
the file containing information has format:
Gas 10000 Home Gas Meter
Electricity 3000 Home Electricity Meter
Flight 8000 Holiday Abroad
Flight 1000 Long Weekend
Car 1250 Own Car (10000 miles)
Car 500 Holiday Hire Car

I would like to put the first column to be stored in an array called -
char fuel [ ]
second column - double amount
third column - char description [ ]

the first and second will be used by a fuction called double co2(char
fuel [ ], double amount)
and the third will be stored in the int main ....

Second Question...Can you return 2 values from a function double
co2(char fuel [ ], double amount)??

Thanks
Chris
Response to the Second Question:
See K&R chapter 1, section 8, page 29

"When the name of an array is used as an argument, the value
passed to the function is the location or address of the beginning of
the array - there is no copying of array elements. By subscripting this
value, the function can access and alter any argument of the array."

Your function will alter the values in the original array.
May 14 '07 #3
Jim Barlow <fo**********@charter.netwrites:
[...]
See K&R chapter 1, section 8, page 29

"When the name of an array is used as an argument, the value
passed to the function is the location or address of the beginning of
the array - there is no copying of array elements. By subscripting
this value, the function can access and alter any argument of the
array."
Which is actually (as I keep repeating) a special case of a more
general rule. Whenever an array name (more generally, an expression
of array type) is used, its value is implicitly converted to a pointer
to its first element, *unless* it's the operand of a unary "&" or
"sizeof" operator, or it's a string literal used to initialize a
character array.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
May 14 '07 #4
On May 14, 3:19 am, "chu...@gmail.com" <chu...@gmail.comwrote:
Hi
Ok...I got a problem in reading a file containning information, and
trying to send it to a function...
the file containing information has format:
Gas 10000 Home Gas Meter
Electricity 3000 Home Electricity Meter
Flight 8000 Holiday Abroad
Flight 1000 Long Weekend
Car 1250 Own Car (10000 miles)
Car 500 Holiday Hire Car

I would like to put the first column to be stored in an array called -
char fuel [ ]
second column - double amount
third column - char description [ ]

the first and second will be used by a fuction called double co2(char
fuel [ ], double amount)
and the third will be stored in the int main ....

Second Question...Can you return 2 values from a function double
co2(char fuel [ ], double amount)??

Thanks
Chris
If the box is Unix.. use

cut -f1 -d' ' to access first column,
In general
cut -fx -d' ' to access xth column,

May 14 '07 #5
ok so far:

#include <stdio.h>
double co2(char fuel[], double amount){

}

//Main program starts here
int main ()
{
char fuel[20];
double amount;
double Total_Sum=0;
char description[40];
char answer;
char ch;
int i;
int y=0;
FILE * fptr;
fptr = fopen("info.txt", "r");
ch = getc(fptr);

//Displays the programs function
printf("\n\n");
printf("This program is designed to caculate the net CO2 emissions
\n");
printf("- Using the information listed in a file\n");
printf("- Read and analyse the information\n");
printf("- Give total CO2 emission in kg and 'rations'\n");
printf("- Provide a breakdown of the emissions\n");
printf("- Make use of the 'srncmp' function\n\n");

//Continue program?
printf("Do you what to start the program?(Y/N) ");
scanf(" %c",&answer);
printf("\n");

//If Yes?
if(answer='Y'){
while ( ch != EOF){
fscanf(fptr,"%19s %lf %39 [^\n]",fuel , &amount,
description);
}
}
return 0;
}

is my fscanf right?? cause I don't think its quite working....

May 14 '07 #6
On 14 May 2007 15:00:40 -0700, "ch****@gmail.com" <ch****@gmail.com>
wrote:
>ok so far:

#include <stdio.h>
double co2(char fuel[], double amount){

}

//Main program starts here
int main ()
{
char fuel[20];
double amount;
double Total_Sum=0;
char description[40];
char answer;
char ch;
int i;
int y=0;
FILE * fptr;
fptr = fopen("info.txt", "r");
ch = getc(fptr);

//Displays the programs function
printf("\n\n");
printf("This program is designed to caculate the net CO2 emissions
\n");
printf("- Using the information listed in a file\n");
printf("- Read and analyse the information\n");
printf("- Give total CO2 emission in kg and 'rations'\n");
printf("- Provide a breakdown of the emissions\n");
printf("- Make use of the 'srncmp' function\n\n");

//Continue program?
printf("Do you what to start the program?(Y/N) ");
scanf(" %c",&answer);
printf("\n");

//If Yes?
if(answer='Y'){
while ( ch != EOF){
fscanf(fptr,"%19s %lf %39 [^\n]",fuel , &amount,
description);
}
}
return 0;
}

is my fscanf right?? cause I don't think its quite working....
I don't think so. The third % needs an 's' somewhere to tell it to
extract a string.
Remove del for email
May 15 '07 #7
On 13 May, 23:19, "chu...@gmail.com" <chu...@gmail.comwrote:
Hi
Ok...I got a problem in reading a file containning information, and
trying to send it to a function...
the file containing information has format:
Gas 10000 Home Gas Meter
Electricity 3000 Home Electricity Meter
Flight 8000 Holiday Abroad
Flight 1000 Long Weekend
Car 1250 Own Car (10000 miles)
Car 500 Holiday Hire Car
Your format is unclear - is it truly columnar data, where each "field"
occupies a specific range of character positions, or is it blank
delimited?

Either way, I'd not use scanf, fscanf or fgets followed by sscanf -
IMHO the whole family of functions is the spawn of satan...

I'm not going to post a solution, as this smells like homework.

On the question of "Can you return 2 values from a function" - the
answer is simply "no". What are you actually trying to do here?

May 15 '07 #8
On 15 May, 02:10, Barry Schwarz <schwa...@doezl.netwrote:
On 14 May 2007 15:00:40 -0700, "chu...@gmail.com" <chu...@gmail.com>
wrote:
....
fscanf(fptr,"%19s %lf %39 [^\n]",fuel , &amount, description);
....
is my fscanf right?? cause I don't think its quite working....
I don't think so. The third % needs an 's' somewhere to tell it to
extract a string.
Nope, it needs the space removing between the 39 and the '['...

May 15 '07 #9
On 14 May, 23:00, "chu...@gmail.com" <chu...@gmail.comwrote:
ok so far:

#include <stdio.h>

double co2(char fuel[], double amount){

}

//Main program starts here
int main ()
{
char fuel[20];
double amount;
double Total_Sum=0;
char description[40];
char answer;
char ch;
int i;
int y=0;
FILE * fptr;
fptr = fopen("info.txt", "r");
ch = getc(fptr);

//Displays the programs function
printf("\n\n");
printf("This program is designed to caculate the net CO2 emissions
\n");
printf("- Using the information listed in a file\n");
printf("- Read and analyse the information\n");
printf("- Give total CO2 emission in kg and 'rations'\n");
printf("- Provide a breakdown of the emissions\n");
printf("- Make use of the 'srncmp' function\n\n");

//Continue program?
printf("Do you what to start the program?(Y/N) ");
scanf(" %c",&answer);
printf("\n");

//If Yes?
if(answer='Y'){
while ( ch != EOF){
And how would this occur?
fscanf(fptr,"%19s %lf %39 [^\n]",fuel , &amount,
description);
}
}

return 0;

}

is my fscanf right?? cause I don't think its quite working....
remove the space after 39...

May 15 '07 #10
ch****@gmail.com wrote:
ok so far:

#include <stdio.h>
double co2(char fuel[], double amount){

}

//Main program starts here
int main ()
{
char fuel[20];
double amount;
double Total_Sum=0;
char description[40];
char answer;
char ch;
int i;
int y=0;
FILE * fptr;
fptr = fopen("info.txt", "r");
ch = getc(fptr);

//Displays the programs function
printf("\n\n");
printf("This program is designed to caculate the net CO2 emissions
\n");
printf("- Using the information listed in a file\n");
printf("- Read and analyse the information\n");
printf("- Give total CO2 emission in kg and 'rations'\n");
printf("- Provide a breakdown of the emissions\n");
printf("- Make use of the 'srncmp' function\n\n");

//Continue program?
printf("Do you what to start the program?(Y/N) ");
scanf(" %c",&answer);
printf("\n");

//If Yes?
if(answer='Y'){
This line contains an error, even though the compiler may not have
warned about it.
In C and C++, the =-sign *always* means assignment, regardless of the
context where it is used.
To test for equality, you need
if (answer == 'Y') {

If you also want to accept a lower-case y, you need to make explicit
provisions for it.
For example:
if (toupper(answer) == 'Y') {
or
if ((answer == 'Y') || (answer == 'y')) {
while ( ch != EOF){
fscanf(fptr,"%19s %lf %39 [^\n]",fuel , &amount,
As others already noted, you must remove the space between 39 and [.
description);
}
}
return 0;
}

is my fscanf right?? cause I don't think its quite working....
The first one is (but the test following it is wrong), but the second
isn't.

Bart v Ingen Schenau
--
a.c.l.l.c-c++ FAQ: http://www.comeaucomputing.com/learn/faq
c.l.c FAQ: http://www.eskimo.com/~scs/C-faq/top.html
c.l.c++ FAQ: http://www.parashift.com/c++-faq-lite/
May 15 '07 #11
"ch****@gmail.com" wrote:
>
.... snip ...
>
Second Question...Can you return 2 values from a function double
co2(char fuel [ ], double amount)??
Sure, if you collect them into a suitable struct.

--
<http://www.cs.auckland.ac.nz/~pgut001/pubs/vista_cost.txt>
<http://www.securityfocus.com/columnists/423>
<http://www.aaxnet.com/editor/edit043.html>
<http://kadaitcha.cx/vista/dogsbreakfast/index.html>
cbfalconer at maineline dot net

--
Posted via a free Usenet account from http://www.teranews.com

May 16 '07 #12

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Harry Zoroc | last post by:
I would like to treat an xsd Schema file as XML file and to display the targetNamespace and all the imports. That's it. But the following does not work. Why? I did not enter the stylesheet in the...
4
by: Shane | last post by:
Thanks in advance for your help. I have created several classes that each have their own member functions that allow them to print the data stored int their respective classes. In other words,...
15
by: NO_Code | last post by:
Hi, I am trying a very simple program on a linux machine. Code given below int main{ char a,b; printf("Enter a "); scanf("%c",&a); printf("Enter b "); scanf("%c",&b);
13
by: RadiationX | last post by:
I have to solve the following problem:Write a program that accepts two integers, and determines if the second is a factor (is evenly divisible into) the first. Here is the code i have so far. ...
3
by: RadiationX | last post by:
Hello, I have a simple print output questioin. What I'm trying to do is print an array of lenth 20 which holds 20 ints in the form of five columns and four rows. like this below 12 80 90 6 ...
2
by: Glen | last post by:
Hello, I've written a script in python and put together a simple QFrame with a QTextBrowser with Designer. I've translated the C++ into python using puic4. The .py file is called outputWin.py. ...
1
by: cbbibleboy | last post by:
Howdy! I'm importing a large file (213 MB) and parsing it. My question is: does the VB6 Line Input read the whole file, then select out each line, or actually only read that line. I thought it...
5
by: 0xception | last post by:
Hi, I'm attempting to create a perl script that will modify a series of RRD databases (a couple hundred of them). in order to do this the RRD database can be exported to XML modified and then...
3
by: kez | last post by:
How do you read from a binary file into a structure that contains multiple vectors? For example struct studentRecords { string user; vector<string> friends; vector<string>...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.