I tried my code below, and i am getting a output of 0. I added lines like cout << p; and cout << alpha to see if the values are getting stored into the variables. My output looks like this -18811398930we are in the loop0 Can anyone help me?? i am newbie to C++ so please take it easy on me. My input files looks like

23
.12
3
35
3
.
.
.
67
1
4

#include<iostream>
#include<fstream>
#include <stdio.h>
using namespace std;


int main()
{

    int p;
    int key; 
    double alpha;
    int searchnum;
    int numkeys;
    int i; 
    ifstream inputfile;

    inputfile.open("input.rtf", ios::in);

    if (!inputfile)
    {
        cout <<"Unable to open file" ;
        exit(1); //terminate 
    }

    inputfile >> p; //first line of the input file

    cout << p; 


    inputfile >> alpha;  //second line of input file

    cout << alpha;

    inputfile >> numkeys; //third line of input file

    for(i = 0; i <(numkeys+1); i++)  //loop 
    {

        inputfile >>  key;
        cout << "we are in the loop";
    }  //end loop

    inputfile >> searchnum;  //the last number used for a search

    cout << searchnum;


    inputfile.close(); 


} //close main

hi, i run your program and got the following output:

230.12we are in the loopwe are in the loopwe are in the loopwe are in the loop4

with input file as:
"input.txt":
23
.12
3
35
3
2
1
4
3
2
3
67
1
4

try changing the .rtf input file to a .txt, or does it have to be an rtf?

I am using xcode to run my program and it doesn't work it, i might have to try another program to run mine. what program are you using?

I am using xcode to run my program and it doesn't work it, i might have to try another program to run mine. what program are you using?

i am doing it in Linux using the default c++ compiler (c++ <sourcefile>) and its running smoothly. by the way i have changed the input file extension to "txt" and then tested.

try changing the .rtf input file to a .txt, or does it have to be an rtf?

needs to be .rtf because thats what apple uses as its texteditor

needs to be .rtf because thats what apple uses as its texteditor

hi, I have tested it as .rtf also. And its still working.

I also hat the same few months ago, so i am glad to help.
The problem is that you failed to initialize your variables.
In c++,if you fail to initialize(i.e assign a value, traditionally 0)to your
variables, the compiler would assign do so,with values of its choice
(on most computers large values e.g 56563858393)
Try to initialize your variables in that code and see the change.
Hope this helped!!

What you need to be doing here is taking a good look at your input file and your code....

The data you've used in your original post contains a few full-stops/periods These are causing the problems you're seeing.
This is your original data:

23
.12
3
35
3
.
.
.
67
1
4

ok, now stepping through your code, what we see is this....
1. you open your file...fine, file opens ok.
2. you read p from the input file, p is set to 23
3. you read alpha from the input file, alpha is set to 0.12
4. you read numkeys from the file, numkeys is set to 3
5. you set up a loop to loop from 0 to numkeys+1.
The loop does this:
1st iteration: assign key the next value in the file. -> key = 35
2nd iteration: assign key the next value in the file. -> key = 3
3rd iteration: assign key the next value in the file.....
But what's this? The next thing from the file is a full-stop/period '.' and not an int...so because key is an int, it's not being assigned anything, so it remains to be 3.
4th iteration: Again, it's another '.' and not an int, so the value of key does not change, the value of key is still 3.

Finally after the loop, you're trying to read another value from the file into the integer variable searchnum.
Once again the file contains a character '.'. Because searchnum is an integer it doesn't get assigned anything, so searchnum remains in it's uninitialised state....Hence the strange number you're seeing.

So the items you've read from your file are:

23 // value of p
.12 // value of alpha
3 // number of keys
35 // 1st key (in loop, i=0)
3 // 2nd key (i=1)
. // 3rd key....Doh! (i=2)
. // 4th key....Doh! (i=3)
. // searchnum...Doh!

The rest of the file is not read.
67
1
4

So what you're seeing is partially due to the data in the file and partially due to the way you are attempting to assign values to your variables. Because there are non-numeric characters in the test file, some of your int variables aren't getting assigned values.
Putting all numeric values in the test file might help. Perhaps replacing the '.'s with 0 or -1 instead?

For example, if you want to know why dkalita's version is working, lets take a look at their test data:

23 // value of p
.12 // value of alpha
3 // value of numkeys
35 // 1st key (in loop, i=0)
3 // 2nd key (i=1)
2 // 3rd key (i=2)
1 // 4th key (i=3)
4 // value of searchnum
3 // not used
2 // not used
3 // not used
67 // not used
1 // not used
4 // not used

All of the items in the file are valid integers, so none of the statements are failing when attempting to assign values from the file to variables in the code.

However, if the '.'s have some significance and they are supposed to be in the file, then perhaps what you need to be doing is using std::getline to get each line from your file as a string and then parse the string to ensure it's fit for purpose before converting the string to float or int, or whatever datatype you need.
e.g. for getting an int value from the file.....

std::string rawData; // temporary string
std::getline(inputfile, rawData); // read line from file
if(isValidInt(rawData)) // check rawData contains a valid int
    yourIntVariable=atoi(rawData.c_str()); // if valid, convert to int and assign to an int variable
else // Doh!
    yourIntVariable=-1; // perhaps use -1 to indicate an error

OK, and then the isValidInt function would look like something this:

bool isValidInt(const std::string& str)
{
    for(std::string::const_iterator iter = str.begin(); iter!=str.end(); iter++)
    {
        if( !std::isdigit((*iter)) )
            return false;
    }
    
    return true;
}

So the above function parses through the passed-in string. If any of the characters are non-numeric, isValid is set to false. Otherwise it returns true.

It might not even be necessary to parse the string, you could just call atoi directly without checking that the string was a valid integer.
e.g.

std::string rawData; // temporary string
std::getline(inputfile, rawData); // read line from file
yourIntVariable=atoi(rawData.c_str()); //convert to int and assign to an int variable

But what that would do is it would convert the strings containing '.'s from your test file to their int equivalent, which would come out as 0 (because '.' is not an int!).

Anyway, whether you choose to alter the structure of your data file or whether you change the way you read in the data and assign it to your variables is up to you!
Cheers for now,
Jas.

lol that helps with another problem i was doing. This problem the input file is just a list of numbers that looks like this

23
.12
11
45
32
12
56
23
6
7
2
3
4
76
12

I also hat the same few months ago, so i am glad to help.
The problem is that you failed to initialize your variables.
In c++,if you fail to initialize(i.e assign a value, traditionally 0)to your
variables, the compiler would assign do so,with values of its choice
(on most computers large values e.g 56563858393)
Try to initialize your variables in that code and see the change.
Hope this helped!!

I tried that way

where i set at the top
int p = 0;
int i = 0;
int numkeys = 0;
and all the other values to 0 at the top. it still doesn't work when i view the output. I think its the Xcode software, i am going to try to compile and run in linux, to see if i can output the values for each variables.

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.