Code Newbie
News     Forums     Search     Members     Sign Up    

My Code Newbie
Username

Password

Articles/Snippets
ASP Classic
ASP.NET
C
C#
C++
HTML / CSS
Java
Javascript
Linux / BSD
Perl
PHP
Python
Ruby
SQL
VB 6
VB.NET

C.N. Friends
  Planet Rome

Link to Us!
Code Newbie
  Code Newbie
    forums
Old 05-11-2005, 11:04 AM   #1 (permalink)
rogue
Registered User
 
Join Date: Apr 2005
Posts: 9
rogue is on a distinguished road
new help with errors on this code (c++)

ok i have been making a rainfall code to show town,country, and rainfall. i got most of it but i am having problems with the strings the error keep coming up about adding two pointers.could someone please look at this and tell me what i have wrong without changing the statement too much.
Thank you

~VALerie
Code:
/*Rainfall*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <conio.h>

/*function protoypes*/

FILE *datain;

void CloseFiles(void);
void TownFooter(void);
void CtryFooter(void);
void NewTownBreak(void);
void NewCtryBreak(void);

/*define and initialize variables*/
char Ctry[10],	prevCtry[10];
char Town[10],	prevTown[10];
int rainfall, stotTown, stotCtry;



/* MAIN PROGRAM */

void main()
{
	if ((datain = fopen("e:\\rainfall.txt","r"))== NULL)
	{
		printf("Cannot open file");
		exit(1);
	}
	system("cls");
	printf("\nAnnual Rainfall by Town / Country");
	printf("\nTown     Country    Inches");
	printf("\n******************************************************");
	fscanf(datain, "\n%s\n%s\n%d",prevCtry,prevTown);
	strcpy(prevCtry, Ctry);    
	strcpy(prevTown, Town);
	printf("\nData for the Country of %s", Ctry);
	while (strcmp(Town,"prevTown"))
	{
	   stotTown = prevTown+Town
	   stotCtry = prevCtry+Ctry
	   gtotal = stotTown+stotCtry
		prinf("%s %s %d", Town, Ctry, rainfall);

	   fscanf(datain, "\n%s\n%s\n%d", stotTown,stotCtry);
	   
	   if (strcmp(prevCtry) && strcmp(Ctry,"xxxx"))
	   {
			NewCtryBreak();
	   }
	   else if (strcmp(prevTown) && strcmp(Town, "xxxx"))
	   {
			NewTownBreak();
	   }
	}
	printf("\n Subtotal for %s %s = %d",prevTown, prevCtry, stotTown);
	printf("\n\n Subtotal for %s = %d", prevCtry, stotCtry);
	printf("\n******************************************************");
	printf("\n\nGrandtotal = %d", gtotal);
   }

	void NewCtryBreak(void)
	{
	  TownFooter();
	  CtryFooter();
	   printf("\nData for the Country of %s\n\n", Ctry);
	}

	void NewTownBreak(void)
	{
		 TownFooter();
	}

	void CtryFooter(void)
	{
	  printf("\n\n Subtotal for %s = %d", prevCtry, stotCtry);
	  printf("\n******************************************************\n");
	   stotCtry = prevCtry+Ctry;
	   strcpy(Ctry);
	}

	void TownFooter(void)
	{
	   printf("\n Subtotal for %s %s = %d",prevTown, prevCtry, stotTown);
	   stotTown = prevTown+Town;
	   strcpy(Town);
	}

Last edited by redhead; 05-11-2005 at 10:26 PM. Reason: Addded [code][/code] tags
rogue is offline   Reply With Quote
Old 05-11-2005, 11:58 AM   #2 (permalink)
Valmont
[code][/code] enforcer
 
Valmont's Avatar
 
Join Date: Mar 2003
Location: Netherlands
Posts: 1,544
Valmont is on a distinguished road
I believe I told you before to read the link below before posting anything:
[READ THIS FIRST] About the standard C++ forum.
__________________
Valmont is offline   Reply With Quote
Old 05-11-2005, 10:47 PM   #3 (permalink)
redhead
Newbie
 
redhead's Avatar
 
Join Date: Jun 2002
Location: Denmark
Posts: 1,721
redhead is on a distinguished road
Code:
05: #include <conio.h>
This is not a system independant header, and it is not needed in this program.
Code:
26:void main()
Please use int main(), it is more polite, any compiler will turn void main() in to int main() anyway..
Code:
33:  system("cls");
Tsk, tsk, tsk.. very not system independant to use a system call..
Code:
37:  fscanf(datain, "\n%s\n%s\n%d",prevCtry,prevTown);
Why search for 3 inputs and only use two, why reference the variables as parse by value, instead of the needed parse by reference..
Code:
44:              stotCtry = prevCtry+Ctry
how do you expect the char* to know anything about a + operator, your code is in C, yet you try and use varius C++ like features.
Code:
48: fscanf(datain, "\n%s\n%s\n%d", stotTown,stotCtry);
Again, you're one argument short, and you are trying to store strings (char*) into int types, another thing, when using the *scanf() functions, you have to be sure you've alocated the space needed, else your program will end up with unexplainable errors.
Code:
50:   if (strcmp(prevCtry) && strcmp(Ctry,"xxxx"))
51:           {
52:             NewCtryBreak();
53:           }
54:         else if (strcmp(prevTown) && strcmp(Town, "xxxx"))
You do realize strcmp() needs atleast two arguments.. both beeing const char* since we are picky here, it is better to use strncmp() since you're second argument is of a specific length.
Code:
62:  printf("\n\nGrandtotal = %d", gtotal);
gtotal was never declared, or calculated or anything...

Code:
81:  stotCtry = prevCtry+Ctry;
82:  strcpy(Ctry);
Again, prevCtry beeing of char* (and thats enough I dont even have to know what type Ctry is) has no refference of any + operator, and strcpy() needs two arguments, the storage pointer, and the intended filling to copy to it..
Code:
88:  stotTown = prevTown+Town;
89:  strcpy(Town);
Read my above description with sed 's/Ctry/Town/g'
__________________
Don't worry Ma'am, We're university students, We know what We're doing.
-----
If you pull the pin, Mr.Grenade would no longer be your friend.
-----
01000111 01101111 00100000 01000011 00100000 00100001
redhead is offline   Reply With Quote
Old 05-14-2005, 05:48 PM   #4 (permalink)
rogue
Registered User
 
Join Date: Apr 2005
Posts: 9
rogue is on a distinguished road
Thanks red. I appreciate you trying to help me understand it . i got a better concept now. I hope to not have to use these posts again.
rogue is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

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

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Perplexing Error on C Code etorres Standard C, C++ 6 03-24-2005 08:16 PM
Techniques for improving ANSI C code asc01001 Standard C, C++ 1 03-18-2005 11:26 PM
edit? anon Lounge 10 11-21-2002 03:02 PM


All times are GMT -8. The time now is 02:09 AM.


Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.0.0 RC8





Copyright © 2000-2008, Milano Interactive
Web Hosting provided by Portal 360 Web Hosting