C++ Questions ( old thread bump) Created 14 years ago2009-07-16 16:11:45 UTC by Striker Striker

Created 14 years ago2009-07-16 16:11:45 UTC by Striker Striker

Posted 14 years ago2009-07-16 16:11:45 UTC Post #270310
Ok I can't post in the old thread because it's too old and the bump request doesn't work because it says " this thread doesn't exist".

So anyway, I have a problem. I'm practicing a little. I want to find the first prime number after a given number, but this program gives results that only make me confuse.

I even included windows.h and math.h just for being curious if it works with them but it doesn't.

Here's the code : [quote]#include <iostream>
#include <math.h>
#include <windows.h>
using namespace std;

int main(){
int numar;
int count;
int a;
bool yo=true;
cout<<"Introdu numarul de la care programul va incepe sa caute cel mai apropiat numar prim(atentie cu numerele foarte mari!)"<<endl;
cin>>numar;
numar=numar+1;
cout<<" "<<endl;
cout<<" "<<endl;
cout<<" "<<endl;
while(yo){
          a=2;
          count=0;
          while(a<numar-1){
                         if(numar%a==0){
                                        count=count+1;
                                        }
                         a++;
                         }
          if(count>0){
                      yo=false;
                      }
          numar=numar+1;

}

cout<<"Primul numar PRIM aflat dupa numarul introdus este "<<numar<<endl;

system("pause");
system("CLS");
return main();
}

[/quote]

I need some serious help :).
Striker StrikerI forgot to check the oil pressure
Posted 14 years ago2009-07-16 17:52:12 UTC Post #270314
I need some serious help
So do the rest if you don't translate the output. And please, PLEASE, indent your code!!

Just a quick random answer:

Make bool IsPrime(). (not included in my example)

a=inputnumber;
while(!IsPrime(a))
{
. a++;
}

Assuming ints.
Posted 14 years ago2009-07-16 18:39:04 UTC Post #270315
I wrote you a quick MATLAB script that does what you want, I wrote it in matlab so you have to actually go through it so you can understand it instead of just use it. I included comments (Anything preceeded by a % are comments).

Should this not look right in the quote I can send you the M-File, or if you prefer it in .txt form (they are essentially the same)

EDIT: Yep, that looks horrible. Give me a minute to add a link.

EDIT2: http://sites.google.com/site/nefariousparable/downloads/prime.txt
Posted 14 years ago2009-07-16 18:56:58 UTC Post #270316
I don't need the program wrote in another language .... I just want to know what I'm doing wrong.
Striker StrikerI forgot to check the oil pressure
Posted 14 years ago2009-07-16 19:38:00 UTC Post #270319
return main();
Where are you learning this junk from? main() is a special function that can't be called from within your program. If you want this, define it as another function, and call it from main().
Penguinboy PenguinboyHaha, I died again!
Posted 14 years ago2009-07-16 20:08:32 UTC Post #270321
First you should clean up your code. That yo variable... you don't need it. Use break; instead. Or even better, while(!count) instead of while(yo) (then you also have to change int count; to int count = 0; or use a do-while).
Also remove the two useless #include.

DiscoStu's idea is good.
int n;
cin >> n;
while(!isPrime(++n));
count << "Result: " << n;
I'm sure you can find an isPrime function somewhere, or make one yourself.
Oskar Potatis Oskar Potatis🦔
Posted 14 years ago2009-07-16 20:38:13 UTC Post #270322
I'm not really following your code. You'll have to loop through every possible divisor from 1 to the square root of the input number to see if it's anything but 1 and itself.
ChickenFist ChickenFist<Witty Title>
Posted 14 years ago2009-07-16 20:39:12 UTC Post #270323
I am assuming he did not actually read through my code. Had he done so he would have noticed that his code for calculating the prime number is botched, badly, and horribly inefficient.

The comments also walk through the process of calculating whether the number in question is prime. The math in calculating whether or not the number is prime will be the same in any case.

As for it being in another language: Look at what both DiscoStu and potatis_invalid wrote for the code. It is exactly the same as what my code shows, only the syntax is slightly different. If you at least know how to write even basic C++, you can write code and script in MATLAB. Understanding it is even easier than that.

Overall, I provided the code to you, The_(c)Striker, so that you yourself can find what you did wrong. Believe me when I say that if you can't debug your own or even someone else's code (if its legible as potatis was quite blunt with - makes it a million times harder to debug) you will never fully understand it.
Posted 14 years ago2009-07-16 21:49:40 UTC Post #270333
Nefarious is right. You really need to learn to debug a program to completely understand what the program does exactly.

Get a pencil and a notepad and go line by line through the code, doing EXACTLY what each and every line alone does - every initialisation, assigned values, comparisons, etc. You will find yourself following the same path as the actual program (without running it) and seeing exactly what happens as you step through each line of code. Remember, only do what the code will do - you will soon find out where your problem is.
Posted 14 years ago2009-07-17 02:32:29 UTC Post #270347
http://caboose.kkore.net/files/prime.cpp

Probably shouldn't be spoon-feeding a solution, but hell, I haven't used C++ in a while and thought it'd be fun to write. :P

But yeah, Striker.. what everyone else said.
You must be logged in to post a response.