[try Beta version]
Not logged in

 
Flow control with structs

Jul 18, 2016 at 6:15am
Hey. I'm having trouble with if else statements and using structs as the condition. Why does it not register the if statement with the comment blocks

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
#include <iostream>
#include <string>
using namespace std;
struct animalInfo {
    string name;
    string type;
    int foodPower;
    int sleepPower;
};

void getData(ifstream & file, animalInfo arr[], int & count);
void output(animalInfo [], int &);

int main(int argc, const char * argv[]) {
    int datasize;
    animalInfo arr[ARRSIZE];
    string fileName;
    char answer;
   
   
    
    cout << "Create new file?" << endl;
    cin >> answer;
    if(answer == 'Y') {
        ofstream yourfile;
        cout << "Enter Name of animal" << endl;
        getline(cin, fileName);
        yourfile.open(fileName.c_str());

    }else {
        ifstream myfile;
        cout << "Enter file name" << endl;
        cin.ignore();
        getline(cin, fileName);
        myfile.open(fileName.c_str());
        getData(myfile, arr, datasize);
    }
    cout << datasize << endl;
    output(arr, datasize);
    if (arr[datasize].type == "Dog") { //program does not go through this
        cout << "Testing if statement" << endl;
    }
    
    return 0;
}

void getData(ifstream & file, animalInfo arr[], int & count)
{
    while (count < ARRSIZE && file >> arr[count].name) {
        file >> arr[count].type >> arr[count].foodPower >> arr[count].sleepPower;
        count++;
    }
    
    
}

void output (animalInfo arr[], int & limit)
{
    for (int i = 0; i < limit; i++) {
        cout << arr[i].name << endl
        << arr[i].type << endl
        << arr[i].sleepPower << endl
        << arr[i].foodPower << endl;
    }
    
    
}


The File reads

1
2
3
4
Snoopy
Dog
70
30

Jul 18, 2016 at 7:49am
Hi,
> Why does it not register the if statement with the comment blocks
Can you elaborate on that?
Jul 18, 2016 at 8:36am
Hi,

program does not go through this


Worse than that it doesn't compile :+) If you compile with cpp.sh (the gear icon top right of the code) with all 3 warnings on, you will see what I mean.

ARRSIZE is not defined anywhere. If defined in main it is not in scope for the functions, send it as an argument.

const std::size_t ARRSIZE = 100;

You need to declare your files correctly, currently they are not associated with any file, look at the example here:

http://en.cppreference.com/w/cpp/io/basic_ifstream/basic_ifstream
http://en.cppreference.com/w/cpp/io/basic_ofstream/basic_ofstream

Some thing like:

ofstream yourfile("Data.txt", std::ios::app);

Also need #include <fstream>

Good Luck !!
Last edited on Jul 18, 2016 at 8:37am
Jul 18, 2016 at 11:27am
At line 15, datasize is unintialized. You need to initialize it to zero.

Once it's initialized correctly, getData() will read the record into arr[0] and set datasize to 1. So far so good.

Then output() prints the arr[0]. Everything is still good.

Then the if statement at line 40 compares arr[1].type to "Dog". Since you read the data into arr[0], arr[1] is still an empty string.
Jul 18, 2016 at 10:23pm
Oh sorry. My program is a larger program that cplusplus won't allow, so ARRSIZE and all the #includes are in the original program. Sorry for that. My problem is that the
1
2
3
 if (arr[datasize].type == "Dog") { //program does not go through this
        cout << "Testing if statement" << endl;
    }

won't run.
Dhayden. When I do run it, the datasize does say it is 1. Will datasize not being initialized cause that issue?
Jul 19, 2016 at 1:33pm
When I do run it, the datasize does say it is 1. Will datasize not being initialized cause that issue?

You just got lucky. The program doesn't define what the initial value should be. You could add or delete code and the value could suddenly change to some random appearing number. You need to initialize it.

To repeat myself, your if statement is looking at the wrong item in the array. You should look in arr[0] or arr[datasize-1] depending on whether you want the first or last item in the array.
Jul 19, 2016 at 6:25pm
Ah Ok I got it. Thanks a lot.
Topic archived. No new replies allowed.