3

I made a program which divides two variables suppose a & b, reads from a text file, but this on a loop coz I wanted to divide these variables 'till it got a quotient of 1, this only applicable for a number that is divisible, and if not divisible it would indicate/prints "Invalid". So far this all Ive got for I am already stuck. Hope someone could give me an advice how to do this.

inside my textfile mytxt.txt. It has one space between two numbers every lines.

27 3
40 4
1000 5
625 5

heres what ive got so far in my code

  #include <iostream>
  #include <fstream>
  int main() {

   std::ifstream file("mytxt.txt");
    if ( file.eof()) return 0;
     int a, b, cnt=0;

      while (!file.eof()) {
            file>>a>>b;
          loop: 
                 std::cout<<a << " ";
                 a/=b;
          if(a%b == cnt) 
                 goto loop;
                 std::cout<<"\n";
                    }
          file.close();
          system("pause");
}

output of this is

27 9 3
40
1000 200 40
625 125 25 5
Press any key to continue . . . 

but it should be like this

27 9 3 1
Invalid
Invalid
625 125 25 5 1

it should have 3/3 to have 1 in the last. What should I have in my code to do this and insert a condition that if its not divisible, it prints "Invalid". thanks in advance. pls excuse my grammar.

kersey
  • 137
  • 1
  • 1
  • 11

5 Answers5

0

You are printing a before you have divided it... And you don't identify when it's "invalid". If you want to print only "Invalid", I would think that you need to store the values you got as divisors, and then print them only when the value gets to 1.

And you should use while(file>>a>>b) instead of while(!file.eof()), as you will run the loop once more than required if you use the latter.

Mats Petersson
  • 126,704
  • 14
  • 140
  • 227
0

The problem is that you divide a and THEN you check if it is still divisible...so, for example, you print "3", then a := 3/3=1 (which is not divisible by 3) and now your loop finishes without having printed "1"...

However, I'd suggest you to use another 'while' loop instead 'goto'.

childerico
  • 89
  • 6
0

From your description:

divide these variables 'till it got a quotient of 1

That means looping until a == 1, not while a%b == 0.

if not divisible it would indicate/prints "Invalid"

So check for divisibility with

if (a%b != 0) {
    // it's invalid
}

I would recommend using looping constructs like for and while, rather than hand-crafting your own spaghetti with goto, and also indenting the code to match its structure, to make the code easier for a human to follow.

Mike Seymour
  • 249,747
  • 28
  • 448
  • 644
0
#include <iostream>
#include <fstream>

int main()
{
    std::ifstream file("Input.txt");
    if (file.eof()) return 0;
    int a, b, c, cnt=0;
    while (!file.eof())
    {
        file>>a>>b;
        c = a;
        while(a > 1)
        {
            if (a % b) break;
            a /= b;
        }
        if (a == 1)
        {
            while(c >= 1)
            {
                std::cout << c << " ";
                c /= b;
            }
        }
        else
        {
            std::cout << "Invalid";
        }
        std::cout << std::endl;
    }
    file.close();
}

We use the first while loop to find if it is possible to reach 1. If it is not possible we print Invalid or we iterate till we get 1 and print all the intermediate results.

Output

27 9 3 1 
Invalid
Invalid
625 125 25 5 1 
thefourtheye
  • 233,700
  • 52
  • 457
  • 497
0

I think this code would do what you want (look at the notes below it for details):

#include <iostream>
#include <fstream>
#include <math.h>

int main() {
    std::ifstream file("mytxt.txt");
    if (file.eof())
        return 0;

    int a, b;
    double tmp;

    while ((file >> a >> b)) {
        if( a == 0 || b == 0 )
            continue;                    

        tmp = log(a) / log(b);
        if (tmp != floor(tmp))
            std::cout << "Invalid";
        else {
            do {
                std::cout << a << " ";
            }while((a/=b) != 1);
            std::cout << "1";
        }
        std::cout << "\n";
    }
    file.close();
    system("pause");
}

First: if you want to check for invalid using the division loop, then you would have to save all values and then check if it reached 1 print them all, otherwise print Invalid.

Second: 'First' is considered a BAD approach, you could use log function instead, check if the logarithm of the a to the base b gives you a non-algebraic integer number then print Invalid. Otherwise go to your division loop and print your number sequence, note that printing 1 is an already-known info that could be omitted.

Third: It worth to add a check on the reading, for example if your file has a new line at its end, you won't exit the loop. So, it is recommended that you put an IO check on the reading stream if(!(file >> a >> b)); you even could make it the main loop check. Also, it is worth to add logical checks on a and b to avoid special values like 0.

Fourth: Generally, using goto statement is considered harmful except in few cases. So, it is better to avoid it as you can. Here are nice articles and discussions about it:

Community
  • 1
  • 1
Devos
  • 142
  • 2
  • 11