Problem writing to file

This is a discussion on Problem writing to file within the C++ Programming forums, part of the General Programming Boards category; First of all, hello, I'm new here. I have been trying to hack together a number tester, which will test ...

  1. #1
    Registered User
    Join Date
    Oct 2007
    Posts
    8

    Problem writing to file

    First of all, hello, I'm new here.

    I have been trying to hack together a number tester, which will test a number to see if it's a prime or not. It should write the result to a file, but it doesn't. I've also been trying to figure out the source of the problem, but I just don't get it.

    Here it is, to it's fullest:


    Code:
    // The program should test a number, defined by user-input, if it's a prime number or not.
    // This through dividing each number below the user-inputted, starting from 2
    // to, let the user-input be k, k-1. If all cases return a remainder, the number first inputted 
    // number is a prime. 
    // If none of the cases have a remainder the number is not a prime. 
    
    #include <iostream>
    #include <cmath>
    #include <fstream>
    
    using namespace std;
    
    
    
    int main ()         
    {
    int true_false, quit; 
    float i, k, result; 
    result = fmod(k, i);
    i = 2;                 
    cin >> k; 
    
    ofstream primefile;
    primefile.open ("Primeer.txt", ios::ate);
    
    if (!primefile.is_open())
    {
        cout << " The file could not be opened. ";
        }
        else
        {
    
    while (i = k - 1)       
    {
    result = fmod(k,i);    
         
     if (result = 0)               
     {                             
               true_false = true_false + 1;    
               break;
               cout << " The following number isn't a prime: " << k;
               primefile << " The following number isn't a prime: " << k;
               primefile.close();
               cout << " Succesfull writing to file: " << primefile;
               }  
               
     if (result > 0)
     {
               true_false = true_false + 0;
               i++;
               }                                  
    
                  }
                    }
      
      
      
                        
    if (i = k - 1, true_false = 0)
    {
          cout << " The following number is a prime: " << k;
          primefile << " The following number is a prime: " << k;
          cout << " Succesfull writing to file: " << primefile;   
              }
    
    
                  
    cin >> quit;            
    if (quit == true)       
     {                      
             return 0;      
             }
    
    
    }

    Hope I didn't screw the formatting up.

    Any help would be appreciated.
    Last edited by Imagine; 10-28-2007 at 02:00 PM.

  2. #2
    Registered User
    Join Date
    Nov 2006
    Posts
    42
    Quote Originally Posted by Imagine View Post
    Code:
    while (i = k - 1)       
    // ...
    if (result = 0)               
    // ...     
    if (i = k - 1, true_false = 0)
    // ...
    Is this really what you meant?

  3. #3
    Sanity is for the weak! beene's Avatar
    Join Date
    Jul 2006
    Posts
    321
    Well, what's the error(s)?
    Last edited by beene; 10-28-2007 at 06:30 AM.

  4. #4
    Registered User
    Join Date
    Oct 2007
    Posts
    8
    What do you mean Jimzy? I think I made a mistake, it should be:

    Code:
    while ( i<k )
    {
      //...
    }
    The problem I'm having is that the program doesn't output any data, whatsoever.
    Last edited by Imagine; 10-28-2007 at 06:41 AM.

  5. #5
    Registered User
    Join Date
    Sep 2006
    Posts
    835
    = is assignment, == is test for equality. For example,
    Code:
    if (result = 0) {
    means set the value of result to 0 and never execute the code block, since the value of the expression "result = 0" is 0, which evaluates to false.

  6. #6
    Registered User
    Join Date
    Nov 2006
    Posts
    42
    As robatino explained.
    By assigning 0 to result, this
    Code:
    if (result = 0) 
    {
      // ...
    }
    won't be true, and code inside this if won't execute. Same story goes for:
    Code:
    if (result > 0)  // result is 0 after previous assignment
    and
    Code:
    if (i = k - 1, true_false = 0)
    Just fix ifs conditions and it should help.

  7. #7
    Registered User
    Join Date
    Oct 2007
    Posts
    8
    Thanks very for pointing that out. Forgot about that.
    Last edited by Imagine; 10-28-2007 at 04:22 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem with Creating File for File Processing
    By Dampecram in forum C Programming
    Replies: 2
    Last Post: 12-07-2008, 12:26 AM
  2. Problem with file writing
    By Goldrak in forum C++ Programming
    Replies: 7
    Last Post: 04-09-2006, 06:46 AM
  3. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  4. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  5. Need a suggestion on a school project..
    By Screwz Luse in forum C Programming
    Replies: 5
    Last Post: 11-27-2001, 01:58 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21