This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
epiccodernamez's profile picture

C++ Nothing to do with CC.

Started by epiccodernamez, 28 January 2013 - 06:37 PM
epiccodernamez #1
Posted 28 January 2013 - 07:37 PM
So I recently started to go on C++ classes and part of my homework is to make a program with "A" and "B" and to switch their number.If a=5 and b=2 I have to output b=2 a=5.That is not hard, I already made it but if I run the program outside "CodeBlocks" It immediately closes after I am done with it. I hope there are some C++ programmers on here. :)/>

Here is my code:

#include<iostream>
using namespace std;
int main(){
long long a, b, c;
cout<<"Vuvedete A:";
cin>>a;
cout<<"Sega vuvedi B:";
cin>>b;
c=a, a=b, b=c;
cout<<"A=";
cout<<a;
cout<<" B=";
cout<<b;
return 0;
}
Lyqyd #2
Posted 28 January 2013 - 07:40 PM
Moved to General.
epiccodernamez #3
Posted 28 January 2013 - 07:40 PM
Moved to General.
I knew its not in the right spot. Thank you! :)/>
theoriginalbit #4
Posted 28 January 2013 - 07:53 PM
so in CodeBlocks this works fine? o.O
epiccodernamez #5
Posted 28 January 2013 - 08:02 PM
so in CodeBlocks this works fine? o.O
Yep, It doesnt close or anything. But without codeblocks it closes.
theoriginalbit #6
Posted 28 January 2013 - 08:05 PM
how are you running it outside of CodeBlocks?

Also as a side note this

cout<<"A=";
cout<<a;
Can be this

cout << "A=" << a;
also there is a new line with

<< endl;

EDIT: Also why are you saying that its a long twice?

long long a, b, c;
And lastly it is good practise when having a main function to have the method declaration like this

int main( int argc, char* argv[] )
this allows runtime arguments, argc is the argument count and argv[] is an array of the arguments
Edited on 28 January 2013 - 07:08 PM
epiccodernamez #7
Posted 28 January 2013 - 08:07 PM
how are you running it outside of CodeBlocks?

Also as a side note this

cout<<"A=";
cout<<a;
Can be this

cout << "A=" << a;
also there is a new line with

<< endl;
Thanks man, I'll add this.
theoriginalbit #8
Posted 28 January 2013 - 08:10 PM
Thanks man, I'll add this.
Probably wouldn't have fixed the problem, (have a read of the edit too)…

So how are you running the code outside of CodeBlocks?
epiccodernamez #9
Posted 28 January 2013 - 08:16 PM
Thanks man, I'll add this.
Probably wouldn't have fixed the problem, (have a read of the edit too)…

So how are you running the code outside of CodeBlocks?
Well CodeBlocks has "Build and run" I click that and it creates an .exe file of the program on my desktop and then I run it like a normal program (double click).
theoriginalbit #10
Posted 28 January 2013 - 08:24 PM
you have to run it through command prompt. it doesn't create a window for you to get input and display output (cin - console in, cout - console out). unless c++ works VERY different on Windows than it does on Linux and Mac :P/>
iownall555 #11
Posted 28 January 2013 - 08:32 PM
Add "cin.get();" (might be something different, it's been a while since I've tried using c++) to the end of your program before the return statement.
The reason why it closes immediately afterwards is because the program has finished. If that makes sense.

By adding "cin.get();" before the return statement, it will show what it has printed to the window and close it when you press enter.

it doesn't create a window for you to get input and display output (cin - console in, cout - console out)
It does create it, however it closes it when it finishes printing to the window.
theoriginalbit #12
Posted 28 January 2013 - 08:39 PM
It does create it, however it closes it when it finishes printing to the window.
Just tested again on mine and it does… haha wow, too long since I've done C++
Orwell #13
Posted 29 January 2013 - 01:38 AM
I always use cin.get(); it should work. On windows you can also use system("PAUSE"); but that's bad practice in general.
Mads #14
Posted 29 January 2013 - 02:15 AM
I don't really like doing it all for you, if it's your homework, but this works:


#include <iostream>

int main(void) {
    int a, b;

    std::cout << "Enter a value for a: ";
    std::cin >> a;
    std::cout << "Enter a value for b: ";
    std::cin >> b;

    int c = a;
    a = b;
    b = c;

    std::cout << std::endl << "These are the new values:" << std::endl << "a = " << a << std::endl << "b = " << b << std::endl;
    std::cin.get();

    return 0;
}
Orwell #15
Posted 29 January 2013 - 02:24 AM
I don't really like doing it all for you, if it's your homework, but this works:
Then why did you do it? :P/> Also, you didn't do it all for him, you barely added std::cin.get(); in there and beautified it a bit (like proper namespace usage). Besides that, it's exactly the same as in the OP. Give him some credit. :P/>