very new to c++, and having trouble... plz help

very new to c++, and having trouble... plz help

Post by l2andyl2hoads on Tue Nov 25, 2008 7:45 pm
([msg=15462]see very new to c++, and having trouble... plz help[/msg])

i recently (today) got "dev c++" to start learning c++. just to get started, i made this code to test things out...
Code: Select all
#include <stdio.h>

int main()
{
    printf("Hello world!\n");
    return 0;
}


keep in mind, i am working with dev c++ and in vista. when i compile it, then go to run it, it comes up for a split second and then goes away... can anyone provide a little bit of troubleshooting please?
l2andyl2hoads
New User
New User
 
Posts: 4
Joined: Tue Nov 25, 2008 7:36 pm
Blog: View Blog (0)


Re: very new to c++, and having trouble... plz help

Post by fashizzlepop on Tue Nov 25, 2008 8:02 pm
([msg=15463]see Re: very new to c++, and having trouble... plz help[/msg])

It works. Nothings wrong with it. It's supposed to do that. Run it from the command line and it won't dissapear.
The glass is neither half-full nor half-empty; it's merely twice as big as it needs to be.
User avatar
fashizzlepop
Developer
Developer
 
Posts: 2304
Joined: Sat May 24, 2008 1:20 pm
Blog: View Blog (0)


Re: very new to c++, and having trouble... plz help

Post by l2andyl2hoads on Tue Nov 25, 2008 8:15 pm
([msg=15464]see Re: very new to c++, and having trouble... plz help[/msg])

first of all, thank you very much for the help.

second... run it from the command line? upon downloading dev c++, they give you example codes. just to see, i opened them and ran them. they work fine and don't go away either. anyway, i digress. what/where is the command line (in dev c++)?
l2andyl2hoads
New User
New User
 
Posts: 4
Joined: Tue Nov 25, 2008 7:36 pm
Blog: View Blog (0)


Re: very new to c++, and having trouble... plz help

Post by Kurushimi on Tue Nov 25, 2008 9:07 pm
([msg=15467]see Re: very new to c++, and having trouble... plz help[/msg])

I know you're problem. The reason it comes up and exits is because when the program is completed, it automatically exits. To stop this, just do this:

Code: Select all
#include <stdio.h>

int main()
{
    printf("Hello world!\n");
    system("PAUSE");
    return 0;
}


the "system()" function takes the string you give it as an argument and makes it as though you wrote it as a command in the command prompt. Writing the "pause" command in the command prompt forces the computer to...well, pause, and wait for you to hit a key. So, the above program will display:

Hello World!
Press any key to continue . . .

====

The second option, as the person before me said, would be to run it from the command prompt. To do this, you simply open command prompt and navigate to the folder where the executable is created after the program is compiled. (your computer should come with help on this if you don't know how to do this). Then simply type the name of your program and hit enter.
Kurushimi
New User
New User
 
Posts: 17
Joined: Thu Oct 23, 2008 6:44 pm
Blog: View Blog (0)


Re: very new to c++, and having trouble... plz help

Post by l2andyl2hoads on Tue Nov 25, 2008 9:21 pm
([msg=15468]see Re: very new to c++, and having trouble... plz help[/msg])

:D thank you very much. I'm pretty decent with computers and some stuff that is brought up here on HTS, but for some reason haven't picked up c++ until today. Once again, thank you for your patience on dealing with such a minute problem
l2andyl2hoads
New User
New User
 
Posts: 4
Joined: Tue Nov 25, 2008 7:36 pm
Blog: View Blog (0)


Re: very new to c++, and having trouble... plz help

Post by Kurushimi on Tue Nov 25, 2008 9:34 pm
([msg=15469]see Re: very new to c++, and having trouble... plz help[/msg])

No problem. This bugged the heck out of me too.
Kurushimi
New User
New User
 
Posts: 17
Joined: Thu Oct 23, 2008 6:44 pm
Blog: View Blog (0)


problem with code?

Post by l2andyl2hoads on Thu Nov 27, 2008 12:58 pm
([msg=15538]see problem with code?[/msg])

i completed the simple code as shown before and made several new ones for some math

now i am trying to combine if statements with the math so that i can choose what operation to perform. i don't know if i have gone about this all wrong, but i have received the "antiquated or deprecated" error. anyway, here is the code. once again, help much appreciated

Code: Select all
#include <iostream.h>

int main()
{
     cout<<"Input two numbers..."<<endl;
     int var1;
     int var2;
     int var3;
     cin>>var1;
     cin.ignore();
     cin>>var2;
     cin.ignore();
     cout<<"What operation would you like to perform? \na=add\ns=subtract\nd=divide\nm=multiply"<<endl;
     char var4;
     cin>>var4;
     cin.ignore();
     if (var4='a')
        var3=var1+var2;
     if (var4='s')
        var3=var1-var2;
     if (var4='d')
        var3=var1/var2;
     if (var4='m')
        var3=var1*var2;
     cout<<var3<<endl;
       
     cin.get();
     return 0;
}



EDIT: ok... so i've changed it a little bit to remove unnecessary code and attempt to fix it (i think)... btw, it "works", but all answers are multiplied though...
l2andyl2hoads
New User
New User
 
Posts: 4
Joined: Tue Nov 25, 2008 7:36 pm
Blog: View Blog (0)


Re: very new to c++, and having trouble... plz help

Post by kSevis on Sat Jan 10, 2009 6:01 pm
([msg=15886]see Re: very new to c++, and having trouble... plz help[/msg])

Hmm, let me rewrite it a little for you... I think all the four ifs are run.

Code: Select all
#include <iostream.h>

int main()
{
  int var1, var2, var3; //Same effect. I find it looks neater if variables like these are declared in the beginning of the file, but it\'s a personal thing.
  char var4;
  cout<<"Input two numbers..."<<endl;
  cin>>var1;
  cin.ignore();
  cin>>var2;
  cin.ignore();
  cout<<"What operation would you like to perform? \na=add\ns=subtract\nd=divide\nm=multiply"<<endl;
  cin>>var4;
  cin.ignore();
  switch (var4) {
    case 'a': //same as your ifs, as far as I understand.
      var3=var1+var2;
      //cout << "Adding" << endl;
      break; //The breaks are there to separate the cases.
    case 's':
      var3=var1-var2;
      //cout << "Subtracting" << endl;
      break;
    case 'd':
      var3=var1/var2;
      //cout << "Dividing" << endl;
      break;
    case 'm':
      var3=var1*var2;
      //cout << "Multiplying" << endl;
      break;
    default:
      cout << "Action not recognised!" << endl;
      break;
  }
  cout<<var3<<endl;
  cin.get();
  return 0;
}


Try this code. If it still shows the same, uncomment the four cout lines I put, and see if it outputs doing all four actions (or less, if you start with subtracting.)

Another way to solve this would be to not declare var3, and instead use:

Code: Select all
var1 += var2;
var1 -= var2;
var1 /= var2;
var1 *= var2;


In place of your var3 = ... lines. In the case that it isn\'t working, it\'ll output var1 as it was if you add, with a slight possible rounding to the closest var2.
Always happy to get (random) PMs/IMs.
kSevis
New User
New User
 
Posts: 8
Joined: Sat Sep 27, 2008 4:06 am
Location: The Netherlands
Blog: View Blog (0)


Re: very new to c++, and having trouble... plz help

Post by BhaaL on Sun Jan 11, 2009 6:31 am
([msg=15932]see Re: very new to c++, and having trouble... plz help[/msg])

kSevis wrote:Hmm, let me rewrite it a little for you... I think all the four ifs are run.


One thing you have to keep in mind with C-style Languages (that are, but not limited to: C++, C#, PHP, Perl, ...) is that there is a difference between = (assignment) and == (comparison).

Writing the following assigns 'a' to var1, and assignments are always true (except when the value assigned evaluates to false):
Code: Select all
char var1 = 'x';
if (var1 = 'a')
//var1 is now 'a'


The following is what you actually wanted, comparing the value of var1 to 'a':
Code: Select all
char var1 = 'x';
if (var == 'a')
//var1 is 'a'
else
//var1 is not 'a'
BhaaL
Poster
Poster
 
Posts: 270
Joined: Sun Apr 13, 2008 11:16 am
Blog: View Blog (0)


Re: very new to c++, and having trouble... plz help

Post by Jack The Ripper II on Sun Jan 11, 2009 1:44 pm
([msg=15959]see Re: very new to c++, and having trouble... plz help[/msg])

Every time i write any C++ code like "#include <stdio.h>

int main()
{
printf("Hello world!\n");
system("PAUSE");
return 0;
}
it says 16 bit MS-DOS Subsystem the NTVDM CPU has encountered an illegal instruction and it has options of close or ignore and either one closes it what do i do?
Jack The Ripper II
New User
New User
 
Posts: 19
Joined: Sun Jan 11, 2009 12:47 am
Blog: View Blog (0)


Next

Return to C and C++

Who is online

Users browsing this forum: No registered users and 0 guests