Problem with a Void function. (Also looking for some ideas)

Hello. Im quite new to C++ and stuffs related to it.

So, i have a homework in university which i cant seem to figure out.
So the task is:

There are 2 files.
lab19no.txt, from which i read the information
and lab19uz.txt where i save the information (Or string, not sure how its called).

I have to read the input from the 1st file and write in the 2nd file all the words from the input that consists of numbers only.

Lets say the input is " abcd.123456.asd2341.1334.d33432."

In this case id need to save to the 2nd file 2 words - 123456 and 1334.

The first problem is, i made a void function to check out, if the char is '.'

1
2
3
4
5
6
void pzime (char a)
{
        if(a == '.' || a == ',' || a == '(' || a == ')' || a == '!' || a == '?') return true;
        else return false;
}


And im getting error : return-statement with a value, in function returning 'void' [-fpermissive]| Im not sure where i went wrong, but id appreciate some help.


About the task. My idea was to First of all count how many symbols are there in the file no1. Then, find out how many words are there. Then to create an array with size of how many words are there (For example array[words]). Then if the word starts and if the symbol isdigit then convert it to int and add it to array[The Word Number Which the Prog Is Currently Checking]. And if it finds in the word at least one symbol that is not digit set the array[particularword] value to 0. And in the end save into the 2nd file all the array[words] values that are not equal to 0.

My version seems way too complicated for me, so id like to find out if anyone has any better/easier method how to do this.

And this is what i came up with... (Cant compile it coz of that void)
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#include <fstream>
#include <iostream>
using namespace std;


void pzime (char a)
{
        if(a == '.' || a == ',' || a == '(' || a == ')' || a == '!' || a == '?') return true;
        else return false;
}


int main ()
{
    char c, previouse = '.';
    bool sakums = false; // Sakums stands for begining of the word.
    bool beigas = false;// Beigas stands for end of the word. 
    int vardi = 0; // Value that represents how many words are there in the strong. 
    bool letters = false;//bool that becomes true if theres a letter inside the word.
    fstream fno ("lab19no.txt", ios::in);
    fstream fuz ("lab19uz.txt", ios::out);
    fno.get(c);
    while (fno)
    {
        ////////////////////////////////////////
        if(pzime(c)== true) // checks if the C is a symbol that represents begining or the end of the word. 
        {
            if (sakums == false && beigas == true)
            {
                sakums == true;
                beigas == false;
            }
            else if (sakums == true && beigas == false)
            {
                vardi++;
                letters = false; 
                beigas == true;
                sakums == false;
            }
            if(pzime(previouse)== true)
            {
                sakums == true;
                beigas == false;
            }
        }
        int words[vardi];
        if(pzime(c)==false && sakumes == true && beigas == false && letters == false)
        {
            if(isdigit(c)==true)
            {
                words[vardi] = words[vardi] * 10 + (c - '0'); // converts c from char to int 
            }
            if(isdigit(c)== false)
            {
                words[vardi] = 0; 
            }   
        }
        for(int i=0; i<vardi; i++)
        {
            if(words[i]!=0)
            {
            fuz.put(words[i]);
            }
            else continue; 
        }
    }
    fno.close ();
    fuz.close ();
    return 0;
}
You tell that your function is void, aka not returning anything. But you are trying to retorn something from it, namely bool value. So compiler complains: you promised that you will not return anything, but you still did. Breaking promises is bad.
Ok, i fixed the function:

1
2
3
4
5
char pzime (char a)
{
        if(a == '.' || a == ',' || a == '(' || a == ')' || a == '!' || a == '?') return true;
        else return false;
}



Does anyone has any idea how to solve my task in a different way?
(Not looking for the code, just the idea).
thanks
1
2
3
char pzime (char a)
//
return true
It still works due to bunch of implicit conversion going on, but it still not correct.
What is the type of true and false?
Integer i suppose?
Not really sure to be honest.
What would be the correct way to do this?

EDIT: Owwww, so instead of

char pzime (char a)
it should be
int pzime (char a)

As it returns integer values?
Last edited on
true false is bool
Topic archived. No new replies allowed.