Isdigit

Does anyone know where in my code can I tell it to multiply the two values I'm receiving from the numbers integers?

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
  int main(){

		ifstream indata;
		ofstream outfile;
		float num1, num2;
		int x, add, mult;
		string line;
		streampos begin;

		indata.open("intinput.txt");
		outfile.open("results.txt",ios::trunc);

		if (!indata) {
			cerr << "****Error file cannot be opened****" << endl;
			exit(1);

		}
		indata >> num1 >>num2;
		begin = indata.tellg();
		indata.seekg(0, ios::beg);
		while (!indata.eof()){
			while (getline(indata, line))
			{
				add = 0, mult = 0;											
				stringstream num1, num2(line);								
				while (num1, num2 >> x)										
					add += x;												
					mult += x;

				outfile << "The numbers added are:" << add << '\n';			
				outfile << "The numbers multiplied are:" << mult << '\n';
				cout << "The numbers added are:" << add << '\n';			
				cout << "The numbers multiplied are:" << mult << '\n';
							
			}
			
		}

		indata.close();
		outfile.close();
		cout << "****Program Completed****" << endl;
		return 0;

}
Last edited on
You are overcomplicating the code to the extreme.

This appears the be related to this thread:
http://www.cplusplus.com/forum/beginner/121876/2/

People there warned you that your code didn't make sense, but instead you made a new thread. I recommend just starting over with your code - it's already too convoluted.
Okay. Any other advice.
I advise that you simplify your design.
Where would you recommend I start?
Start here:
1
2
3
4
5
6
7
8
#include <iostream>
#include <fstream>
#include <string>

int main()
{
    //...
}
I can't help further without knowing what you are trying to accomplish - I can only guess from your old program.
Assuming that, as in the original thread, you're actually just extracting pairs of numbers, the solution could be as simple as:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <fstream>
#include <iostream>

int main()
{
    // std::ifstream fin("intinput.txt");
    // std::ofstream fout("results.txt");

    // std::istream& in = fin ;
    // std::istream& out = fout ;
    std::ostream& out = std::cout; 
    std::istream& in = std::cin;

    int a, b;
    while (in >> a >> b)    // while two ints are successfully extracted
    {
        out << a << " + " << b << " = " << a + b << '\n';
        out << a << " x " << b << " = " << a * b << '\n';
    }
}


Note that looping on eof is usually the wrong thing to do and that's true in your case.

In your code above, the snippet:
1
2
3
4
5
6
7
8
9
10
11
12
        while (!indata.eof()){
            while (getline(indata, line))
            {
                add = 0, mult = 0;
                stringstream num1, num2(line);
                while (num1, num2 >> x)
                    add += x;
                    mult += x;

                // ...
            }
        }


is equivalent to:

1
2
3
4
5
6
7
8
9
10
11
            while (getline(indata, line))
            {
                add = 0, mult = 0;       // mult should be 1 here if you're going to multiply.
                stringstream num2(line); // num1 may as well not exist.
                while (num2 >> x)
                    add += x;

                mult += x;  // notice that this line is not controlled by the while construct.
                            // Also guessing you meant to multiply here and not add.
                // ...							
            }
Last edited on
I haven't learned about the & signs yet so I'm not sure of their function, and the reason I had the + sign in with the mult int is that the * sign would not allow the program to function the way it was written. Also why does the code disregard the num1 integer the way I have it written?
Thank you for the feed back.
Last edited on
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
int main(){

		ifstream indata;
		ofstream outfile;
		float num1, num2;
		int x, add, mult;
		string line;
		streampos begin;

		indata.open("intinput.txt");
		outfile.open("results.txt",ios::trunc);

		if (!indata) {
			cerr << "****Error file cannot be opened****" << endl;
			exit(1);

		}
		indata >> num1 >>num2;
		begin = indata.tellg();
		indata.seekg(0, ios::beg);
		while (getline(indata, line))
			{
				add = 0, mult = 1;											
				stringstream num1, num2(line);								
				while (num1, num2 >> x)										
					add += x,mult *= x;

				outfile << "The numbers added are:" << add << '\n';			
				outfile << "The numbers multiplied are:" << mult << '\n';
				cout << "The numbers added are:" << add << '\n';			
				cout << "The numbers multiplied are:" << mult << '\n';
							
			}
			
		
		indata.close();
		outfile.close();
		cout << "****Program Completed****" << endl;
		return 0;

}


Here is my most current code, it is doing 90% of what I want, however I need to have the program check the incoming values to ensure that they are in fact real numbers, not letters or decimals or blank spaces. I have been reading up on isdigit and thing that is the way I want to go. However when I use it in the format of.if(isdigit(num1)); if I put it after line 24 it tells me I must have an integer not a stringstream value. if I put it after line 18 it tells me none of my vales are numbers. Is there anyone who can please give me some assistance?


Last edited on
If you continue to ignore our attempts at helping you, we will stop making attempts at helping you.
Oh, yes I did mean to tell you thank you for all of your help. So what about the question can you give me any insight?
Cire gave you a completely working program that does what you want, and explained why your old program made no sense. You then proceeded to ignore it because there was a symbol you didn't understand.

In your defense, I thought I replied explaining what was going on, but the post seems to have not gone through for some reason. Lines 6-12 of cire's code just allow you to easily switch between I/O with the console and I/O with files. You can completely remove lines 9-12 and just uncomment lines 6-7 if you want.
I completely believe that you could make the program for me in a fraction of the time. As well as better, however what would that do in helping me learn how to code. So with that said, I have a few questions according to your post I can remove the part of the program that opens the file it's self. According to every thing that i have read that is the first step in reading from the file, so is there a way to combine operations then?
I think there is some misunderstanding going on here, either I am misreading your posts or you are misreading mine.
slour wrote:
I haven't learned about the & signs yet so I'm not sure of their function,

A std::ostream& is a reference to std::ostream. A reference may refer to any class which is derived from the type named in the declaration. For instance, a std::ostream& can refer to an object of type std::ostream, such as std::cout or an object of type std::ofstream such as fout in my code above.

I don't have the file that you're working with, so I made it easier to test my code with console input. You could easily do a search replace in and out with std::cin/std::cout or fin/fout. There's nothing magical going on there.


slour wrote:
and the reason I had the + sign in with the mult int is that the * sign would not allow the program to function the way it was written.

That doesn't make much sense to me. Perhaps being more explicit with actual (attempted) code would help.


slour wrote:
Also why does the code disregard the num1 integer the way I have it written?

When you use the comma operator, the first of the comma separated expressions is evaluated and the result is discarded. Then the next expression is evaluated and the value of the statement becomes the value of the last expression evaluated.

So, in: (Extra parentheses added for clarity)

1
2
        while ((num1), (num2 >> x))
            add += x,mult *= x;


num1 is evaluated and discarded and the value of the control expression for the while loop becomes just num2>>x.


slour wrote:
I have been reading up on isdigit and thing that is the way I want to go.

For most programs such as this simply trying to extract a number from the input stream and noting whether the extraction succeeded or failed is enough. isdigit operates on individual characters. Feeding it a std::stringstream is not feeding it an individual character.
LB, I was confused last night I think I might have been trying to answer a different question. cire, so if I understand you correctly from your last post my program should not have ever worked as I wanted it to. But I have to say with the way the code is written above it does function with regards to adding and multiplying, I guess I must have just gotten lucky. So to elaborate a little more I need my program to also check and ensure that it has a proper real number to use in the formula, is the isdigit code not the right way to go?
Also I tried this code and had no output to the file at all.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <fstream>
#include <iostream>
using namespace std;

int main()
{
	ifstream fin("intinput.txt");
	ofstream fout("results.txt");

	
	int a, b;
	while (fin >> a >> b)
	{
		fout << a << "+" << b << "=" << a + b << '\n';
		fout << a << "*" << b << "=" << a * b << '\n';
	}

}
Last edited on
cire?
Topic archived. No new replies allowed.