PDA

View Full Version : My First Program!


Sleazy P Martini
Feb 19th, 2002, 08:49 PM
I'm really excited! We just started C, C++ and I got to write my first program. It's nothing too exciting but to me it's step one in a cool direction. :)

/*My first program
Author: Andrew Gregory
Date: Feb 19 2002
Purpose of program: To learn how */

#include <stdio.h>

main()
{
int integer1, integer2, sum;

printf("Enter first integer\n");
scanf("%d", &integer1);
printf("Enter second integer\n");
scanf("%d", &integer2);
sum = integer1 + integer2;
printf("sum is %d\n", sum);

return 0;
}

BowevelJoe
Feb 19th, 2002, 09:26 PM
eek...err..what does it teach you how to do lol?!

Sleazy P Martini
Feb 19th, 2002, 09:52 PM
Well the program just adds a couple numbers. Thats it. The "To learn how" documentation was just filling in stuff that my teacher wanted. I could have put anything there.

BowevelJoe
Feb 20th, 2002, 08:02 AM
ah...i see...hmm one of these days im going to get into 3d stuff and make some characters for some games, and ill mail em to you if you want em...and that has nothing to do with the thread..Oops :D

LiquidSm0ke
Feb 20th, 2002, 08:57 AM
Congratulations Sleazy! :)

Now that we're on the topic however, I just made this one:

#include <iostream>
#include <string>

using namespace std;

// Example of using the generic algos of find and replace
bool
replace ( string &s, const char * find_str, const char * rep_str )
{
unsigned int i = 0;
if (( i=s.find(find_str)) != s.npos)
{
s.replace(i, strlen(find_str), rep_str);
return (true);
}
return(false);
}

void
String_Test ( void )
{
// Create instance of a string from literal string
string s = "Hello World";

// Output of the string in various ways
cout << "s.c_str() = " << s.c_str() << endl;
cout << "s.data() = " << s.data() << endl;
cout << "s = " << s << endl;
cout << "sizeof(s) = " << sizeof(s) << endl;
cout << "s.size = " << s.size() << endl;
cout << "s.length = " << s.length() << endl;
cout << "s.capacity = " << s.capacity() << endl;
cout << "s.max_size = " << s.max_size() << endl;

// output by single char
int len = s.length();
cout << "s[0]-s[" << len << "] = ";
for ( int i=0; i<len; i++ )
{
cout << s[i];
}
cout << endl;

// Creating a string
string s2;
// assignment to a string
s2 = "Hello Mars and Venus";

// concatenate strings together
string s3;
s3 = s + " " + s2;
cout << s3 << endl;

// test for empty string
cout << "s.empty() << " << s.empty() << endl;

//create and empty string
string s4;
cout << "s4.empty() << " << s4.empty() << endl;

//Inline replacement in the string, found
string s5 = "Address: ADD_DATA";
cout << "s5 = " << s5 << endl;
if ( replace(s5, "ADD_DATA", "212 Oak Lane") )
{
cout << "s5 = " << s5 << endl;
}

//Inline replacement in the string, not found
string s6 = "Address: ADD_DATA";
cout << "s6 = " << s6 << endl;
if ( replace(s6, "XYZ", "212 Oak Lane") )
{
cout << "s6 = " << s6 << endl;
}
else
{
cout << "s6 = " << "Not Found" << endl;
}

if ( s5 == s6 )
{
cout << "s5 == s6 = true" << endl;
}
else
{
cout << "s5 == s6 = false" << endl;
}

string s7 = "Test";
string s8 = "test";

if ( s7 < s8 )
{
cout << "Test < test= true" << endl;
}
else
{
cout << "Test < test= false" << endl;
}
}

int
main ( void )
{
String_Test();
return(0);
}

It's not done, and there are some errors. :o

Sleazy P Martini
Feb 20th, 2002, 11:50 AM
Whoa! Whats that one do? I've only been exposed to C for one day so that looks confusing to me.

LiquidSm0ke
Feb 20th, 2002, 01:17 PM
It's just a command for processors. It's not confusing, you'll get it once you get to Alogarithms.

Sleazy P Martini
Feb 20th, 2002, 02:18 PM
I am having big time trouble with Algorithms. I just don't know where to start. They give me a headache.

Reid
Feb 20th, 2002, 06:39 PM
Is anyone here familiar with logic circuits?
I'm working on things involving that in a high school class right now, and it's losing me.

LiquidSm0ke
Feb 20th, 2002, 08:53 PM
Originally posted by Sleazy P Martini
I am having big time trouble with Algorithms. I just don't know where to start. They give me a headache.

lol, I know what you mean, alogorithms suck.