My thing isn't working!

I'm trying to create a simple phone number program. It doesn't have much functionality yet, but I'll add that later.

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
#include <iostream>
#include <string>
using namespace std;


int main()
{
     int numberOfNumbers = 0;
     string numbers[1000];
     string names[1000];

    void printPhoneNumbers()
    {

        for(int i=0;i<=numberOfNumbers;i+i)
        {
            //cout << names[i];
        }
    }
    
    void enterNumber(string numb, string name)
    {
            numbers[numberOfNumbers] = numb;
            names[numberOfNumbers] = name;
            numberOfNumbers += 1;
    }

    return 0;
}


When I run it, it produces an error:
1
2
3
4
5
6
7
/home/zargy/Documents/Coding/Learning C++/LearnC++2/main.cpp||In function ‘int main()’:|
/home/zargy/Documents/Coding/Learning C++/LearnC++2/main.cpp|13|error: a function-definition is not allowed here before ‘{’ token|
/home/zargy/Documents/Coding/Learning C++/LearnC++2/main.cpp|8|warning: unused variable ‘numberOfNumbers’ [-Wunused-variable]|
/home/zargy/Documents/Coding/Learning C++/LearnC++2/main.cpp|9|warning: unused variable ‘numbers’ [-Wunused-variable]|
/home/zargy/Documents/Coding/Learning C++/LearnC++2/main.cpp|10|warning: unused variable ‘names’ [-Wunused-variable]|
/home/zargy/Documents/Coding/Learning C++/LearnC++2/main.cpp|29|error: expected ‘}’ at end of input|
||=== Build finished: 2 errors, 3 warnings ===|
You need to declare your other functions outside of the main() and then call the functions inside like so:
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
#include <iostream>
#include <string>
using namespace std;


void printPhoneNumbers()
{
    for(int i=0;i<=numberOfNumbers;i+i)
    {
        //cout << names[i];
    }
}

void enterNumber(string numb, string name)
{
        numbers[numberOfNumbers] = numb;
        names[numberOfNumbers] = name;
        numberOfNumbers += 1;
}

int main()
{
     int numberOfNumbers = 0;
     string numbers[1000];
     string names[1000];

    printPhoneNumbers();

    for (int i = 0; i < 1000 ; i++)
        enterNumber(numbers, names);

    return 0;
}


You're only problem with my code here is getting the data into the functions. You'd either need to declare global variables or pass your arrays as arguments.

Change void printPhoneNumbers()

to

void printPhoneNumbers(string* numbers, int size)

and you can just pass the array of numbers along with the size in your main

printPhoneNumbers(numbers, numberOfNUmbers);


Just make sure you increment numberOfNumbers when you add a number. Right now, you aren't doing that. ( Well you kinda are but not correctly)
Last edited on
Topic archived. No new replies allowed.