Click here to Skip to main content
15,885,244 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
It works in the first instance of an odd number, but as soon as the user inputs another one it outputs Magic Box# >(their number)<. I this bit of code only to receive odd numbers. Please help me.

C
#include<iostream>
#include<fstream>
#include<iomanip>
using namespace std;

ofstream outfile;
ifstream infile;
int main()
{
    outfile.open("output.txt");
    infile.open("numbers.txt");
    int n, row, column; //intermediate variable and row and column variables
    cout << "Welcome to  the Magic Square Program!" << endl << endl;
    infile >> n;
        if (n % 2 == 0)
        {
            cout << "Odd numbers only, Please enter an odd number:" << endl;
            cin >> n;
            goto Odd;
        }
        else
        {
            Odd:
            cout << "Magic Box#: " << " > " << n << " <" << endl << endl;
        }


        int MagicSquare[3][3];
        MagicSquare[0][2] = 1;

        for (int x = 0; x < n; x++)
        {
            for (int y = 0; y < n; y++){
            //  cout << setw(3) << x * y << MagicSquare[x][y];
            }
            cout << endl;
        }

system("pause");
    return 0;
}
Posted
Updated 23-Feb-15 14:09pm
v2
Comments
Sergey Alexandrovich Kryukov 24-Feb-15 0:01am    
Oh-oh, C++ with goto... Are you serious? And goto across "if" branches. Do you think anyone will volunteer even to look at it? Let's see... :-)
Better write some non-nonsense code if you want to be taken seriously.
—SA
Trevon Harris 24-Feb-15 16:06pm    
Thanks for nothing buddy, assuming you only mocked me I can tell you probably didn't know how to answer the question yourself.
Sergey Alexandrovich Kryukov 24-Feb-15 16:17pm    
Just one idea: all members can read and make some conclusions on who you are. Huh?
—SA
Trevon Harris 24-Feb-15 16:19pm    
we're ending this conversation here.
Andreas Gieriet 24-Feb-15 12:50pm    
This code is so broken - one could also say "sick".
I wonder from which corner you come, i.e. what your background is.
Who showed you using "goto"?! You have to have gotten it from somewhere...
The other thing is the use of streams. Why from numbers.txt you scan the first number from and then you request more from cin...
Suggestion: go through any C++ online tutorial and you get the basics. We cannot teach you the very basics of C++ here.
Regards
Andi

1 solution

1. never ever use goto in C++

2. Replace your input code with this:
C++
int n;
do {
   cout >> "Plase enter an odd number: " << endl;
   cin >> n;
} while (n%2 != 0);


3. once it works as intended, flesh out that code with some extra checks, like negative numbers, or non-numeric input
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 24-Feb-15 16:44pm    
Sure, a 5. :-)
—SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900