A little program

Hello!
I tried to make a little program but i can't understand why it doesn't start correctly, the console close immediatly, i'm using Visual studio.

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
  #include "stdafx.h"
  #include <iostream>

int main()
{
	int n, k;
	printf("Décomposition d’un nombre en facteurs premiers\n");
	printf("Introduire un nombre supèrieur à zéro:");
	scanf("%d", &n);
	while (n > 0)
	{
		printf("Facteurs premier: ");
		while (n % 2 == 0);
		{
			printf("2 \n ");
			n /= 2;
		}
		k = 3;
		while (n > 1)
		{
			if (n%k != 0) { k += 2; }
			else { printf("%d", k); n /= k; }
		}
		return 0;
		}
I guess you use Visual Studio since you use stdafx.h so one option is to run it with Ctrl+F5
or have a look at http://www.cplusplus.com/forum/beginner/1988/

BTW Since this seems a C program it's better to remove #include <iostream> and use
#include <stdio.h>
Start it without debugging. Debug -> Start without Debugging. :)
Last edited on
I still can't make it start correctly, maybe i'm using the wrong template but i can't find one that work
It is very hard to tell from your indenting - and it may be a cut-and-paste error when uploading your code ... but you are, strictly, missing a final curly brace }

... and you have a spurious semicolon at the end of line 13
Last edited on
add a read into N at the end of your program. Run the program, and do not enter a value for this input until you have looked at the program's output. If it is correct, the program is simply finishing fast and you can't see the output.
Try this one @Inpachi. (Remember to de-comment the stdafx.h line if using Visual Studio.)

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
//#include "stdafx.h"                  // DE-COMMENT IF USING VISUAL STUDIO
#include <iostream>                    // C++ style, but use <cstdio> if you want to revert to printf, scanf
#include <string>                      // added, but only to assist line 28
using namespace std;                   // EITHER ADD THIS HERE ...
                                       // ... OR QUALIFY EVERYTHING THAT NEEDS IT (e.g. std::cout)
int main()
{
   int n, k;
   cout << "Decomposition of a number into prime factors\n";
   cout << "Input a number greater than 0: ";
   cin >> n;
// while( n > 0 )                      // DELETE (unless it's a slightly unnatural way of checking input)
// {                                   // DELETE
      cout << "Prime factors: ";
      while ( n % 2 == 0 )             // REMOVE THE SEMICOLON ; ****
      {
         cout << 2 << " ";
         n /= 2;
      }
      k = 3;
      while ( n > 1 )
      {
         if ( n % k != 0 ) { k += 2; }
         else              { cout << k << " ";   n /= k; }
      }
// }                                  // DELETE

   string dummy;   cout << "\nPress enter";   getline( cin, dummy );   // simulated wait-for-enter

// return 0;                          // moved, but not needed
}                                     // FINAL BRACE MUST BE ADDED    ******* 
Last edited on
Or try this one - closest to your original.
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
#include "stdafx.h"
#include <stdio.h>
#include <conio.h>

int main()
{
  int n, k;
  printf("Décomposition d’un nombre en facteurs premiers\n");
  printf("Introduire un nombre supèrieur à zéro: ");
  scanf("%d", &n);
  printf("Facteurs premier: ");
  while (n % 2 == 0)
  {
    printf("2 \n ");
    n /= 2;
  } /* while (n % 2 == 0) */ 

  k = 3;
  while (n > 1)
  {
    if (n % k != 0) 
    { 
      k += 2; 
    }
    else 
    { 
      printf("%d", k); 
      n /= k; 
    }
  } /* while (n > 1) */

  _getch();
  return 0;
}
}
@lastchance this one work thank you
@Thomas1965 Yes this one is closer to the example in my workbook but i can't make it to work
i'm starting to think i'm doing something wrong with visual studio i just can't make "printf" and "scanf" to work even in the other example

i want to thank everyone, i'm student through correspondence and its sometime hard alone
i just can't make "printf" and "scanf" to work even in the other example

What is the problem? Error messages ?

Try this. Go to File->New->Project...->Visual C++->Win32 Console Application
Click next
In the next window check and uncheck the following options
https://pasteboard.co/GIjFGni.jpg
Click finish.
In the Solution explorer on Source right-click and choose Add->New Item->C++ File, call it main.cpp and click add.
Paste your code into main.cpp, remove #include "stdafx.h" and save it.
Press Crtl+F5 to run.
its working this time, i'm still trying to understand why because it similar to what i did before
i can make my others programs to work too now, i think that i checked a wrong option my bad
Topic archived. No new replies allowed.