Hi everyone, I'm new at C (just started this week), and I'm reading tutorials on line without the help of a book or class. I was wondering if there are common practices most tutorials teach that really aren't good to get into.
The tutorial I'm reading now says to use int main(), and then at the end to return 0; however, I was reading some of the code snippets on this site and found many people have other things such as main(void) or some other heading with void in it.
There was also an article I believe that said not to use scanf, but instead you should use gets (or something along those lines.)
I know my question isn't the most direct but any advice on common practices or maybe a recommended C tutorial would be great. :-) Thanks for any replies.
Hey everyone,
I am not sure how to ask this, but this is what I am wondering.
I am new to JS and am looking at a lot of webpages' sources. The thing that keeps coming up that I can't figure out is when you click a button (whatever it may be: next, submit, etc) how does it compare the input value to whatever it has. The button is given the input type = 'submit' but it doesn't explain where its destination is or what its comparing inputed data with.
Hope I explained well enough.
p.s. Thanks for any replies, and I have tried to look this question up; however, appears to be to vague.
Here you go. Also as a side note, system ("pause") is a bad habit.
#include <iostream>
using namespace std;
int main ()
{
string password;
cout <<"Please input a password to enter successfully: ";
cin>>password;
if (password == "pen15")
cout <<"Welcome\n\n";
else
cout <<"invalid"<<endl;
cin.get();
return 0;
}
I liked your program. Dont get offended or anything (just wanted to make sure, because people seem to do that sometimes.) I am also new at programming and I wanted to say thank you because I didnt know about the toupper command until I read your thread. Anyways, I wanted to see if I could revise your script into a smaller one. However it is still your program that is the backbone of it (I didnt really change anything, I just condensed it, I dont know if this is frowned upon in this community (what i have done) but if it is I'm sorry.) Here is what I came up with:
//Created by Greg Schader revised by Valley
#include <iostream>
#include <string>
using namespace std;
int main()
{
char function = ' ';
int firstNum = 0;
int secondNum = 0;
int answer = 0;
string show= "Answer of operation is: \n";
cout << "\n\nEnter the letter of the operation you wish to perform (a/s/m/d): ";
cin >> function;
cout << "\nEnter the first number of the operation: "<<endl;
cin >> firstNum;
cout << "\nEnter the second number of the operation: "<<endl;
cin >> secondNum;
switch (toupper(function))
{
case 'A':
answer = firstNum + secondNum;
cout <<show<< answer;
break;
case 'S':
answer = firstNum - secondNum;
cout <<show<< answer;
break;
case 'M':
answer = firstNum * secondNum;
cout <<show<< answer;
break;
case 'D':
answer = firstNum / secondNum;
if (firstNum==0)
cout<<"Inappropriate Opperation!\n";
else cout <<show<< answer;
break;
default:
cout<<"Illegal …
Hey everyone,
well I wrote this program because I haven't seen anyone do it yet (it has always been random numbers, not sentences.) Also I just wanted to see if I could do it :). Im posting this not because I need it fixed, although I'll tweak it if I find a better way, I'm just wondering how YOU would create this program (produce random strings.) Im kinda postive there is a more elegant way to write this code seeing as I kinda JUST started programming.
Curious of other ways. Thanks :)
// I wrote this so I could make my conversation program
// seem less one dementional.
// And well I haven't seen anyone write a program that produced random sentences.
#include <iostream>
using namespace std;
int main()
{
char answer='y';
cout<<"Would you like to chat? (y/n) ";
cin>>answer;
if(answer=='y')
{
srand(time(0));
int randSent=(rand()%4+1);
if (randSent==1)
cout<<"\nSentence one goes here.\n";
else if (randSent==2)
cout<<"\nSentence two goes here.\n";
else if (randSent==3)
cout<<"\nSentence three goes here.\n";
else if (randSent==4)
cout<<"\nSentence four goes here.\n";
}
else cout<<"ok bye";
return(0);
}
thanks everyone.. i know it wasnt that hard of a program probably for you that have been doing this, but it was difficult for me, and i appreciate everone's help.. here is the finished one (well the revised one).. test it out plz :-).. tell me what you think, be honest..
i might be stupid because it took me till 3 am to get the program completely working but i learned a lot of extra information that i can add to future programs :-D.. and now im going to go crash into my bed... thanks again
// Conversation
#include <iostream>
#include <string>
using namespace std;
int main()
{
string feeling;
char reason[50];
string name;
const int speed=70;
int actualspeed;
cout<< "\n\nWho am i talking to? :-) \t";
cin>>name;
cout<< "\nHey "<<name<< "! Finally!! Ive been waiting all day to talk to you. \n"<<endl;
cout<< "So how are you feeling!?! :-D \n\n\n";
cin>>feeling;
cout<<"\nAnd why are you feeling "<<feeling<<"?\t";
cin.ignore();
cin.getline(reason,50);
cout<<"\nOk now I understand."<<endl;
cout<<"\nDid you do some driving today? ";
cout<< "\n1 - Yes";
cout<< "\n2 - No";
int answer;
cout<<"\nChoice: ";
cin>>answer;
switch (answer)
{
case 1:
cout<< "\n What road did you drive most on "<<name<<"?\n\n";
cout<< "1 - Burnt Store Road\n";
cout<< "2 - 75\n";
cout<< "3 - 41\n";
cout<< "4 - Other";
int choice;
cout<<"\n Choice: ";
cin>>choice;
switch (choice)
{
case 1:
cout<<"\nYou better be careful with that road!\n";
break;
case 2:
cout<<"\nSoo.. What speed were you going? ";
cin>>actualspeed;
if (actualspeed>speed) …
thanks.. sorry for before
i dont understand why getline wont work for my compiler.. i tried to use that as well and it just skipped over the command..
// Conversation
#include <iostream>
#include <string>
using namespace std;
int main()
{
string feeling;
char reason[50];
string name;
const int speed=70;
int actualspeed;
cout<< "\n\nWho am i talking to? :-) \t";
cin>>name;
cout<< "\nHey "<<name<< "! Finally!! Ive been waiting all day to talk to you. "<<endl;
cout<< "So how are you feeling!?! :-D \n\n";
cin>>feeling;
cout<<" And why is that? ";
cin.getline(reason,50);
cout<<"\nOk now I understand."<<endl;
cout<<"\nDid you do some driving today? ";
cout<< "\n1 - Yes";
cout<< "\n2 - No";
int answer;
cout<<"\nChoice: ";
cin>>answer;
switch (answer)
{
case 1:
cout<< "\n What road did you drive most on "<<name<<"?\n\n";
cout<< "1 - Burnt Store Road\n";
cout<< "2 - 75\n";
cout<< "3 - 41\n";
cout<< "4 - Other";
int choice;
cout<<"\n Choice: ";
cin>>choice;
switch (choice)
{
case 1:
cout<<"\nYou better be careful with that road!\n";
break;
case 2:
cout<<"\nSoo.. What speed were you going? ";
cin>>actualspeed;
if (actualspeed>speed)
cout<<"\nYou really shouldnt be speeding.";
else if (actualspeed==speed)
cout<<"\nNot many people go the actual speed.";
else if (actualspeed<speed)
cout<<"\nGoing below the speed limit can cause accidents!";
break;
case 3:
cout<<"I got a couple of warnings on that road. Make sure ";
cout<<"you are driving responsibly.\n";
break;
case 4:
cout<<"Hmm well i have no idea where you are. What do you think i am?";
cout<<" A GPS? Well i dont …
Post the code. We can't comment on what we can't see.
im new at programming and advice is well appreciated
thanks for the website on system("pause")
keep in mind i just started :(, so its probably going to look retarded to most of you.. i wanted to see if i could create a program that would talk to me.. i would like to add more questions, just not yet..
the problem is (well the one i see) is that if the person types in more than one word for the reason, the program wont work..
// Conversation
#include <iostream>
#include <string>
using namespace std;
int main()
{
string feeling;
char reason[10];
string name;
const int speed=70;
int actualspeed;
cout<< "\n\nWho am i talking to? :-) \t";
cin>>name;
cout<< "\nHey "<<name<< "! Finally!! Ive been waiting all day to talk to you. "<<endl;
cout<< "So how are you feeling!?! :-D \n\n";
cin>>feeling;
cout<<"\nAnd why are you feeling "<<feeling<<"?\t";
cin>>reason;
cout<<"\nOk now I understand."<<endl;
cout<<"\nDid you do some driving today? ";
cout<< "\n1 - Yes";
cout<< "\n2 - No";
int answer;
cout<<"\nChoice: ";
cin>>answer;
switch (answer)
{
case 1:
cout<< "\n What road did you drive most on "<<name<<"?\n\n";
cout<< "1 - Burnt Store Road\n";
cout<< "2 - 75\n";
cout<< "3 - 41\n";
cout<< "4 - Other";
int choice;
cout<<"\n Choice: ";
cin>>choice;
switch (choice)
{
case 1:
cout<<"\nYou better be careful with that road!";
break;
case 2:
cout<<"\nSoo.. What speed were you going? ";
cin>>actualspeed;
if (actualspeed>speed)
cout<<"\nYou really shouldnt be speeding.";
else if (actualspeed=speed)
cout<<"\nNot many people go the actual speed.";
else if(actualspeed<speed)
cout<<"\nGoing below the speed limit can cause accidents!";
break;
case 3: …
i need to know the commands... i got the program all situated.. i just need to know what to do with that one sentence... i have if/while/switch and all that in there..
hey. i was looking at your scipt for the program, and i well i didnt know about the system("pause").. thanks
OK man i fixed all your bugs and cleaned it a bit...
u also might want to pick different variable names so its easier to understand...
#include <iostream>
#include <string>
using namespace std;
int main()
{
string home, guest;
int hnr, bnr;
cout << "Title" << endl;
cout << "\n----------------" << endl;
cout << "Write name of home team: "; cin >> home;
cout << "How many goals did home team make? "; cin >> hnr;
cout << "Write name of guest team: "; cin >> guest;
cout << "How many goals did guest team make? "; cin >> bnr;
if (hnr > bnr)
{
cout << home << " won against " << guest<<".\n";
cout << "The result was " << hnr << "-" << bnr;
}
else if (hnr < bnr)
{
cout << guest << " guest won over " << home << ".\n";
cout <<"Final result " << hnr << "-" << bnr;
}
else if(hnr == bnr)
{
cout << "\nTie between " << home << " and " << guest << ". The match ended with "
<< hnr << "-" << bnr;
}
return 0;
}
Im new at C++ and am trying to teach myself. I want to ask the user of the program why they are feeling a certain way, but not do anything with the sentece. So its like
-how are you feeling?-
user -good-
-why are you feeling good?-
user- blah blah blah-
-ok i understand now-