Page 1 of 1 [ 16 posts ] 

Blake_be_cool
Veteran
Veteran

User avatar

Joined: 6 May 2008
Age: 27
Gender: Male
Posts: 860
Location: Australia, NSW, Sydney

19 Jan 2011, 4:14 am

I finally made something straight from scratch and it work how I wanted it to!

Quote:
/*
Name: Project1
Copyright:
Author: Blake James Reilly
Date: 19/01/11 17:26
Description:
*/


#include <iostream>

using namespace std;

int main (int argc, char *argv[])

{
string a, b, c, d;
a = "a";
string answer;

cout << "Select the first letter of the alphabet: " << endl;
cout << "a, b, c, d" << endl;
cin >> answer;

if (answer == a)
{
cout << "This is the right answer!" << endl;
}
else
{
cout << "Sorry this is the wrong answer." << endl;
}
system ("PAUSE");
}

/*Program Ended*/


Yes it is only very simple and basic.
But I'm proud that I did all of this from scratch...


_________________
"Not everything that steps out of line, and thus 'abnormal', must necessarily be 'inferior'."
- Hans Asperger (1938)


Helixstein
Veteran
Veteran

User avatar

Joined: 18 Apr 2010
Gender: Male
Posts: 1,744
Location: New Zealand

19 Jan 2011, 4:15 am

Well done, Blake. :thumleft:


_________________
"We accept the love we think we deserve."


betamaxx
Raven
Raven

User avatar

Joined: 18 Sep 2010
Gender: Male
Posts: 120

19 Jan 2011, 5:20 am

thats good:)
is this your first operational program, if so how many attempts did you make at it?



Blake_be_cool
Veteran
Veteran

User avatar

Joined: 6 May 2008
Age: 27
Gender: Male
Posts: 860
Location: Australia, NSW, Sydney

19 Jan 2011, 7:26 am

betamaxx wrote:
thats good:)
is this your first operational program, if so how many attempts did you make at it?


6th


_________________
"Not everything that steps out of line, and thus 'abnormal', must necessarily be 'inferior'."
- Hans Asperger (1938)


leejosepho
Veteran
Veteran

User avatar

Joined: 14 Sep 2009
Gender: Male
Posts: 9,011
Location: 200 miles south of Little Rock

19 Jan 2011, 8:12 am

Congratulations!


_________________
I began looking for someone like me when I was five ...
My search ended at 59 ... right here on WrongPlanet.
==================================


Biokinetica
Toucan
Toucan

User avatar

Joined: 8 Dec 2010
Age: 35
Gender: Male
Posts: 266
Location: Vulcan

19 Jan 2011, 12:04 pm

If you want, I can show you how to make that a switch statement that will look much cleaner and make things like this easier to edit later. If you're still interested after that, I'll show you how to make it a function call that will lower the amount memory the program needs (not that it's at all large).



kxmode
Supporting Member
Supporting Member

User avatar

Joined: 14 Oct 2007
Gender: Male
Posts: 2,613
Location: In your neighborhood, knocking on your door. :)

19 Jan 2011, 12:15 pm

Image



Blake_be_cool
Veteran
Veteran

User avatar

Joined: 6 May 2008
Age: 27
Gender: Male
Posts: 860
Location: Australia, NSW, Sydney

19 Jan 2011, 5:50 pm

Biokinetica wrote:
If you want, I can show you how to make that a switch statement that will look much cleaner and make things like this easier to edit later. If you're still interested after that, I'll show you how to make it a function call that will lower the amount memory the program needs (not that it's at all large).


That would be awesome!


_________________
"Not everything that steps out of line, and thus 'abnormal', must necessarily be 'inferior'."
- Hans Asperger (1938)


nodice1996
Supporting Member
Supporting Member

User avatar

Joined: 3 Jan 2008
Age: 27
Gender: Male
Posts: 1,047
Location: Michigan

19 Jan 2011, 7:38 pm

I once tried to do something similar in perl script, but it never worked. I deleted the code when I switched from ubuntu to debian.


_________________
Guns don't kill people--Magic Missiles Do.


Blake_be_cool
Veteran
Veteran

User avatar

Joined: 6 May 2008
Age: 27
Gender: Male
Posts: 860
Location: Australia, NSW, Sydney

19 Jan 2011, 8:13 pm

nodice1996 wrote:
I once tried to do something similar in perl script, but it never worked. I deleted the code when I switched from ubuntu to debian.


Do you know If I can still program C++ on Debian?
I've done A bit of search on it a while ago, and if I can still program on it I'll use it.


_________________
"Not everything that steps out of line, and thus 'abnormal', must necessarily be 'inferior'."
- Hans Asperger (1938)


nodice1996
Supporting Member
Supporting Member

User avatar

Joined: 3 Jan 2008
Age: 27
Gender: Male
Posts: 1,047
Location: Michigan

20 Jan 2011, 7:16 am

Blake_be_cool wrote:
nodice1996 wrote:
I once tried to do something similar in perl script, but it never worked. I deleted the code when I switched from ubuntu to debian.


Do you know If I can still program C++ on Debian?
I've done A bit of search on it a while ago, and if I can still program on it I'll use it.

It should work, but if you're moving from windows to *nix you'll need to recompile.


_________________
Guns don't kill people--Magic Missiles Do.


LordoftheMonkeys
Veteran
Veteran

User avatar

Joined: 15 Aug 2009
Age: 34
Gender: Male
Posts: 927
Location: A deep,dark hole in the ground

20 Jan 2011, 8:50 am

Yes, it's a wonderful feeling when after a long, frustrating session of trying to get code to work, it finally comes out perfectly. I have had a lot of these progasms after coding in C.


_________________
I don't want a good life. I want an interesting one.


Biokinetica
Toucan
Toucan

User avatar

Joined: 8 Dec 2010
Age: 35
Gender: Male
Posts: 266
Location: Vulcan

21 Jan 2011, 12:08 am

If you copy/paste this code into DevC++, it will compile as is.

Quote:
#include <iostream>
#include <string>
using namespace std;

int main(){
string a, b, c, d;
a = "a";
char answer; /*since you're only storing one character in "answer", you can just use the char data type.
This is also true for your other string variables, but it's not a problem here.
There's also an ascii hack for the cases, but that's really unnecessary */

cout << "Select the first letter of the alphabet: " << endl;
cout << "a, b, c, d" << endl;
cin >> answer;

switch (answer){

case 'a'://between the single quotes go your "options", or the possible values of var answer.

cout << "This is the right answer!" << endl;//after the colon, the code that you want to run if that case is true

break;//The "break" ends a loop. It has to come after a switch case unless there are multiple right answers to the question.

case 'b'://from here on, you can put in any

cout << "Sorry this is the wrong answer." << endl;

break;

case 'c':

cout << "Sorry this is the wrong answer." << endl;

break;

case 'd':

cout << "Sorry this is the wrong answer." << endl;

break;

//the default statement can be one line, or as many as you want provided you have the braces enclosing everything properly to define the scope
default:
{//A default statement MUST be present in order for the program to compile; can't get rid of it.
}

}
system ("PAUSE");
exit(0);//Same as "return 0;" in intmain()
}


Extra note: Strings are a special type of character data that can handle more than one character. The data type "char" is typically used (but not really mandatory) if you're only using one character to act as a variable.



Blake_be_cool
Veteran
Veteran

User avatar

Joined: 6 May 2008
Age: 27
Gender: Male
Posts: 860
Location: Australia, NSW, Sydney

21 Jan 2011, 2:09 am

Biokinetica wrote:
If you copy/paste this code into DevC++, it will compile as is.

Quote:
#include <iostream>
#include <string>
using namespace std;

int main(){
string a, b, c, d;
a = "a";
char answer; /*since you're only storing one character in "answer", you can just use the char data type.
This is also true for your other string variables, but it's not a problem here.
There's also an ascii hack for the cases, but that's really unnecessary */

cout << "Select the first letter of the alphabet: " << endl;
cout << "a, b, c, d" << endl;
cin >> answer;

switch (answer){

case 'a'://between the single quotes go your "options", or the possible values of var answer.

cout << "This is the right answer!" << endl;//after the colon, the code that you want to run if that case is true

break;//The "break" ends a loop. It has to come after a switch case unless there are multiple right answers to the question.

case 'b'://from here on, you can put in any

cout << "Sorry this is the wrong answer." << endl;

break;

case 'c':

cout << "Sorry this is the wrong answer." << endl;

break;

case 'd':

cout << "Sorry this is the wrong answer." << endl;

break;

//the default statement can be one line, or as many as you want provided you have the braces enclosing everything properly to define the scope
default:
{//A default statement MUST be present in order for the program to compile; can't get rid of it.
}

}
system ("PAUSE");
exit(0);//Same as "return 0;" in intmain()
}


Extra note: Strings are a special type of character data that can handle more than one character. The data type "char" is typically used (but not really mandatory) if you're only using one character to act as a variable.


I don't understand most of this.


_________________
"Not everything that steps out of line, and thus 'abnormal', must necessarily be 'inferior'."
- Hans Asperger (1938)


Biokinetica
Toucan
Toucan

User avatar

Joined: 8 Dec 2010
Age: 35
Gender: Male
Posts: 266
Location: Vulcan

21 Jan 2011, 2:29 am

I guess it's the commenting that's making it look scary. They're meant to explain the code, and in an IDE, they'll be color-coded like this.

Not to mention the formatting. I intentionally inserted extra spacing to make it readable.



Avarice
Veteran
Veteran

User avatar

Joined: 5 Oct 2009
Age: 31
Gender: Male
Posts: 1,067

27 Jan 2011, 1:41 am

Well done! It's always a nice feeling when a piece of code works. Unless it works on the very first try, then it doesn't feel quite right.

My first real program (i.e. One that does something that I need done more than once) was this. I still use it. If you have so time, you should try writing a problem to convert from C to F (or F to C.) It's a program you're going to need eventually, and it's nice to know that it's there and you made it.

Code:
/*Program designed to convert Fahrenheit temperatures to Celcius temperatures.*/

#include<stdio.h>
#include<stdlib.h>

int main()
{
    char fah1[50]; char i[50];
    int fah2; int celc; int i2;   
   
    printf("Would you like to convert a value from Fahrenheit to Celcius?(1=yes, 2=no)");
    gets(i);
    i2=atoi(i);
   
    while(i2==1)
    {
            if(i2!=1)
            {
                     return 0;
            }
            printf("Temperature in Celcius: ");
            gets(fah1);
            fah2=atoi(fah1);
   
            celc=(fah2*9/5) + 32;
   
            printf("Temperature in Fahrenheit: %d",celc);
            getchar();
            printf("Would you like to convert another value? (1=yes, 2=no)");
            gets(i);
            i2=atoi(i);
           
           while(i2==1)
            {
                    if(i2!=1)
                    {
                            return 0;
                    }
            printf("Temperature in Celcius: ");
            gets(fah1);
            fah2=atoi(fah1);
   
            celc=(fah2*9/5) + 32;
   
            printf("Temperature in Fahrenheit: %d",celc);
            getchar();
            printf("Would you like to convert another value? (1=yes, 2=no)");
            gets(i);
            i2=atoi(i);
              }         
   
    }
}


Yes. I know I probably shouldn't have used gets. Also, remember you can use [code] tags on this site, you don't need to use quotes.