Hi i'm new to coding and am having a problem with a program i'm making, i have got it down to 4 errors which are on a single line, but i have no idea was wrong and how to solve it.

#include "stdafx.h"
#include <iostream>
#include <stdlib.h>
#include <string>
#include <fstream>

using namespace std;

# define MAX 100

struct addr {
             char name[30]   ;
             char street[40] ;
             char town[20]   ;
             char county[20] ;
             char code[10]   ;
            } list[MAX];       



void enter(), save(), load();
int del(), search(), show_list(), find_free();
int menu_select(), find(char *name), display(int i), init_list(), inputs(char *prompt , char *s , int count);
int main()
{
	int choice;

	init_list();

    for(;;) 
	{
      choice = menu_select();
	switch(choice)
		{
	case '1': enter();
		break;
	case '2':del();
		break;
	case '3':search();
		break;
	case '4':show_list();
		break;
	case '5':save();
		break;
	case '6':load();
		break;
		}
	cout << "\n";
	}

	return 0;
}

void enter()
{ 
	int i;

    for(;;) {
      i = find_free();
      if(i<0) {
        printf("list full\n");
        return;
      }
      inputs("enter name: ", list[i].name,30);
      if(!*list[i].name) break;
      inputs("enter street: ", list[i].street, 40);
      inputs("enter town: ", list[i].town, 20);
      inputs("enter county: ", list[i].county, 20);
      inputs("enter Postal code: ", list[i].code, 10);
    }

}
int del()
{
    int i;
    char str[255];

    inputs("enter name: " , str , 30);
    i = find(str);
    if (i>=0) *list[i].name = '\0'   ;
    else      printf("not found\n")  ;
}

int search()
{
    int i;
    char name[30];

    inputs("enter name to find: " , name , 30);
    if ((i=find(name))<0) 
      printf("not found\n");
    else
      display(i);
}


int show_list()
{
    int i;

    for(i=0; i&lt;MAX; i++) 

	{
      if(*list[i].name)
	  {
        display(i);
        printf("\n\n");
      }
    }

    printf("\n\n");
}


void save()
{
    FILE *fp;

    if ( (fp=fopen("mlist" , "wb")) == NULL) {
      printf("cannot open file\n");
      return;
    }
    printf("\nsaving file\n");
    fwrite(list , sizeof list , 1 , fp);
    if (ferror(fp)) 
      printf("An error occurred while writing file.\n");
    fclose(fp);
}

void load()

{
    FILE *fp;

    if ( (fp=fopen("mlist" , "rb")) == NULL) {
       printf("cannot open file\n");
       return;
    }
    printf("\nloading file\n");
    fread(list , sizeof list , 1 , fp);
    if (ferror(fp)) 
       printf("An error occurred while reading file.\n");
    fclose(fp);
}
int find_free()
{
    register int i;   
    for(i=0; i<MAX; i++) 
      if(!*list[i].name) return i;  
    return -1;
}

int menu_select()
{
    char s[80];
    int c;
    printf("1. Enter a name\n")  ;
    printf("2. Delete a name\n") ;
    printf("3. List the File \n");
    printf("4. Search\n")        ;
    printf("5. Save the file\n") ;
    printf("6. Load the file\n") ;
    printf("7. Quit\n")          ;

    do {
        printf("\nEnter your choice: ");
        gets(s);
        c = atoi(s);
       } while(c<0 || c>7);

    return c;
}


int find(char *name)
{
    int i;  
    for(i=0 ; i<MAX ; i++) 
      if(!strcmp(name ,list[i].name)) break;
    if(i==MAX) return -1;
    else return i;
}

int display(int i)
{
    printf("%s\n" , list[i].name);
    printf("%s\n" , list[i].street);
    printf("%s\n" , list[i].town);
    printf("%s\n" , list[i].county);
    printf("%s\n" , list[i].code);
}

int init_list()

{
    register int i;   
    for (i=0 ; i<MAX ; i++) 
      *list[i].name = '\0';
}

int inputs(char *prompt , char *s , int count)
{
    char str[255];

    do {
        printf(prompt);
        gets(str);
        if(strlen(str)>=count) printf("\ntoo long\n");
       } while(strlen(str)>=count);

    strcpy(s , str);
}

the main errors are in with "for(i=0; i&lt;MAX; i++)" this part of the code but i can't see whats wrong.

the errors are:
missing ')' before identifier 'i'
syntax error : ';'
syntax error : ')'
syntax error : missing ';' before '{'


I've tried just typing what it says is wrong and that jsut ends up giving me more errors so there's prob something basic i'm missing but i've been trying for hours to sort it out with no luck, hopfully someone can help.

thanks

Recommended Answers

All 8 Replies

You know at least this is not C but rather C++, right?

OP> the main errors are in with "for(i=0; i&lt;MAX; i++)" this part of the code but i can't see whats wrong. for ( i = 0; i < MAX; i++) is the correct syntax.

Yea sorry i was looking around on the site and posted it here not on C++ part by mistake.

I tried to change it to (i=0; i< MAX; i++)

But when i do it gives me errors on most of my functions saying they 'must return a value', when i void the functions the program runs but nothing happens, i type in a menu number and it just repeats the menu over and over, whereas it should let me enter the data and store it etc. Is this because of this change or is the other probs? if so if you wouldn't mind pointing me in the correct direction you really help me out.

OP> But when i do it gives me errors on most of my functions saying they 'must return a value', when i void the functions

Do you know what these are at the beginning of the program?

void enter(), save(), load();
int del(), search(), show_list(), find_free();
int menu_select(), find(char *name), display(int i), init_list(), inputs(char *prompt , char *s , int count);

I think there the prototypes of the function setting the return type and name.

I think there the prototypes of the function setting the return type and name.

As well as the parameters that it would take if it takes any. Allowing the compiler to check that the function is properly defined.

Let's look at this function prototype then.

int display(int i);

returns an integer and accept an integer.

What do we have defined?

int display(int i)
{
    printf("%s\n" , list[i].name);
    printf("%s\n" , list[i].street);
    printf("%s\n" , list[i].town);
    printf("%s\n" , list[i].county);
    printf("%s\n" , list[i].code);
/* missing the int return */
}

It declares the proper integer return but in the body of the function nothing is returned.
Nevertheless, in actuality it doesn't need to return anything. Making possible for us to change the definition as this.

void display(int i)
{
    printf("%s\n" , list[i].name);
    printf("%s\n" , list[i].street);
    printf("%s\n" , list[i].town);
    printf("%s\n" , list[i].county);
    printf("%s\n" , list[i].code);
}

And then the prototype so the compiler doesn't complain.

void display(int i);

Now, you have many of those. Go and make the changes.
By the way, it is not a good idea to mix C syntax with C++. This code is mostly C syntax in a C++ template.

Sorry maybe i should have been more clear, after making the first change to the orignal mistake that you noticed, the compiler then gave me the errors as i had functions as int but without returns, just like you mention but when i change all functions without returns to voids, as well as the prototype to void, the program runs, while the program is running it asks to enter the menu number and then just goes around in a loop, not doing anything but displaying the menu again and asking for another menu number.

Again sorry if i didn't make this clear.

Ok i found the prob that was causeing the menu to loop it was that i forgot to take out the quote marks from the cases, works great now. But just wondering if anyone can help with the fact that when entering data in i what to enter one persons data and then go back to the menu, but at the moment you have to double press enter to return, and if theres anything i can look into to making the program any better.

thanks for the help Aia that got me going cheers

gets(s);

That function is a menace. It is not possible to stop it from reading more than it should, even if the input is greater than the buffer is set to hold.

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.