• Log in or sign up for an account here: https://www.sythe.org/login

first program

  • Thread starter Thread starter darion123
  • Start date Start date
D

darion123

Guest
well its my first program WITHOUT a tutorial i know it kinda newbie but w/e ill learn

Code:
#include<iostream>
using namespace std;
int main(void)
{
    system("TITLE Easy MATH!© - ZeRo");
    char cChar;
    double dnm1 = 0.0;
    double dnm2 = 0.0;
    double dnm3 = 0.0;
    double dnm4 = 0.0;
    double dnm5 = 0.0;
    double dnm6 = 0.0;
    double dnm7 = 0.0;
    double dnm8 = 0.0;
    double dnm9 = 0.0;
    char cDoagain;
    cout << "How much numbers would you like to add (2,3,4,5,6,7,8,or 9)?\n";
    cin >> cChar;
    switch (cChar)
{
    case '2':
         cout << "Two numbers you would like to add!\n";
    cin >> dnm1;
    cin >> dnm2;
    cout << "The answer is " << (dnm1 + dnm2) << "!" << endl;
    break;
    
    case '3':
         cout << "Three numbers you would like to add!\n";
    cin >> dnm1;
    cin >> dnm2;
    cin >> dnm3;
    cout << "The answer is " << (dnm1 + dnm2 + dnm3) << "!" << endl;
    break;
    
    case '4':
         cout << "Four numbers you would like to add!\n";
    cin >> dnm1;
    cin >> dnm2;
    cin >> dnm3;
    cin >> dnm4;
    cout << "The answer is " << (dnm1 + dnm2 + dnm3 + dnm4) << "!" << endl;
    break;
    
    case '5':
         cout << "Five numbers you would like to add!\n";
    cin >> dnm1;
    cin >> dnm2;
    cin >> dnm3;
    cin >> dnm4;
    cin >> dnm5;
    cout << "The answer is " << (dnm1 + dnm2 + dnm3 + dnm4 + dnm5) << "!" << endl;
    break;

case '6':
         cout << "Six numbers you would like to add!\n";
    cin >> dnm1;
    cin >> dnm2;
    cin >> dnm3;
    cin >> dnm4;
    cin >> dnm5;
    cin >> dnm6;
    cout << "The answer is " << (dnm1 + dnm2 + dnm3 + dnm4 + dnm5 + dnm6) << "!" << endl;
    break;
    case '7':
         cout << "Seven numbers you would like to add!\n";
    cin >> dnm1;
    cin >> dnm2;
    cin >> dnm3;
    cin >> dnm4;
    cin >> dnm5;
    cin >> dnm6;
    cin >> dnm7;
    cout << "The answer is " << (dnm1 + dnm2 + dnm3 + dnm4 + dnm5 + dnm6 + dnm7) << "!" << endl;
    break;
    case '8':
         cout << "Eight numbers you would like to add!\n";
    cin >> dnm1;
    cin >> dnm2;
    cin >> dnm3;
    cin >> dnm4;
    cin >> dnm5;
    cin >> dnm6;
    cin >> dnm7;
    cin >> dnm8;
    cout << "The answer is " << (dnm1 + dnm2 + dnm3 + dnm4 + dnm5 + dnm6 + dnm7 + dnm8) << "!" << endl;
    break;
    case '9':
         cout << "Nine numbers you would like to add!\n";
    cin >> dnm1;
    cin >> dnm2;
    cin >> dnm3;
    cin >> dnm4;
    cin >> dnm5;
    cin >> dnm6;
    cin >> dnm7;
    cin >> dnm8;
    cin >> dnm9;
    cout << "The answer is " << (dnm1 + dnm2 + dnm3 + dnm4 + dnm5 + dnm6 + dnm7 + dnm8 + dnm9) << "!" << endl;
    break;
    
default:
        cout << "ONLY UP TO 9 NUMBERS AND IT MUST BE HIGHER THAN 1 !\n";
        break;
        }
        system ("PAUSE");
        system("CLS");
        cout << "Done!\n";
        system("PAUSE");
        return 0;
        }




watcha think?
 
Member of the Month Winner
Sythe's 10th Anniversary
I fixed your post to include
Code:
 tags.

It's extremely lossy. You created so many variables - learn to use arrays. Also, for the purpose of cross-platform porting, avoid using the system() function until you absolutely need to use it. Neither Linux nor Mac have a "pause" or "cls" command in their terminal. I am a frequent user of Linux.

Here's my take:
[code]#include <iostream>

using namespace std;

int main()
{
    int choice;

    printf("Amount of numbers to add:");
    scanf("%d", &choice);

    if(choice <= 0)
    {
        printf("Invalid operand. Requires positive non-zero value.\nTerminating.\n");
        return 1;
    }

    double nums[choice];

    for(int i = 0; i < choice; i++)
    {
        printf("Enter number %d:", i+1);
        cin >> nums[i];
    }

    double output;
    if(choice<=1) output = nums[0];
    else
        for(int i = 0; i <= choice; i++) output += nums[i];

    printf("Output is: %f.\nTerminating.", output);

    return 0;
}

I used cin for the input of the array because I couldn't get the scanf() function to work with it for some reason.
 
Its good for a first try but personally I agree with swan on using an array instead of all those varibles. just declare and initialize it like this
double dnm[9] = {1, 2, 3, 4...}; weres those numbers are the values you want to put into each element(you would put 9 different numbers in there).
It saves you alot of time like that. Also again like what he said, instead of using pause to stop your program and see the results simply create a batch file in the same directory as your program. When you click on the batch file a command prompt should open up, type your programs name in as a command and it should execute and then stop.

To create a batch file just simply open a notepad and type in 2 simple lines
start
end
save your file within qoutes like this "example.bat" remember to save it in ANSI format because I am unsure that the cmd line utility can read unicode or some other text format.

that should pause your program for as long as you like.

If you don't want to type in the full name of your program everytime then there is an alternative that I know, however the output of your program will not appear on the command prompt.

instead of entering in the 2 lines in your batch file enter in just the name of your program and this symbol:

Example >> log.txt

this should create a file called log.txt in the same directory and append the output of your program into it. The ">>" is called a pipe, it is supported in windows and unix. There a four different kinds of pipes that I know of, two for input and two for output.The ">>" looks for a file called log.txt if it does not find it, it creates a file and writes the output of the program to it. remember that in this example, "example" is the name of your program not the batch, by saying example a mean call it whatever you like.
to see the output of your program just click on log.txt.
 
Swan said:
I used cin for the input of the array because I couldn't get the scanf() function to work with it for some reason.
You probably weren't sending it to a variable by reference which is what it requires. ex: &variableName
 
Runescape Gamblingrunescape gambling
Back
Top