Soving an equation with a loop and writing to a file

So i have sadly picked a subject at uni that i just cant understand. This probably sounds like a dumb question but i need help with my code because i don't know what i've done wrong.

The question was:
Write a program that asks the user for two positive integer values, N and M, and determine the following sum using a loop:

Summation: S= N*M Summation k=N (3k+1)

The program should write the above sum (S) to a file named “Totalsum.txt” and to the screen

If the user enters non-­positive values, the program should
ask the user to re-enter these two numbers.

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
#include <iostream>

using namespace std;

int main()
{

float N;
float M;
float S;
float k;
int decider;
k=N;
outputFile.open("Totalsum.txt");
decider = N;

cout<<"Please enter the value of N: ";
cin>> N;
cout<<"Please enter the value of M: ";
cin>> M;

if (!(cin >> N))
        cout << "Invalid integer." << endl; //If a letter is entered
    
    else  (N < 0);
    
    cout <<"ERROR: Please eneter a positive number. "<< endl; //If a nagative number is entered
    
    return 0;
    
    if (!(cin >> M))
        cout << "Invalid integer." << endl; //If a letter is entered
    
    else  (M < 0);
        
        cout <<"ERROR: Please eneter a positive number. "<< endl; //If a nagative number is entered
    
    return 0;

While (decider < N*M)
 S=3*k+1

cout<<"The value of S is " << S << "\n";
outputFile.close();


I'm pretty sure i've done something wrong either with my maths or my coding.
Last edited on
I'm pretty sure i've done something wrong either with my maths or my coding.

I'm pretty sure you can supply a better description of what's happening that makes you think that. Your code is riddled with syntax errors. Look at the error messages generated when you try to compile it and fix the first one, and anything you think might be related to it, then attempt to recompile. Rinse. Repeat.

Hint: There is no else () construct in the language and you're not allowed to spell cout three different ways.
Topic archived. No new replies allowed.