Simple Welding of two small C/C++ Programs, But How??

B

Basil Fawlty

Hi everyone, I'm a newbie to C/C++ and I need some help with a minor C
program. My assignment is to take two programs and mesh them together. The
1st program I wrote compiles and works fine. The 2nd program is to add to
the 1st program with an if_then_else statement, and it's code is also
correct as well; I just don't know how to or where to put it into the rest
of the 1st program to make it work as one. I've tried to modify it but I
get compiler errors. I look forward to any help some kind soul can provide.
The code with both pieces is below. Thanks, Basil Fawlty of Fawlty Towers



FYI - I'm using Notepad to edit the text and a free C/C++ compiler yanked of
the Internet via Download.com called Digital Mars.



#include <stdio.h>

main()

{

int age;

float weight;

char first[15], last[15]; /* 2 char arrays */



printf("\nWhat is your first name? ");

scanf(" %s", first); /* No ampersand on
character strings */

printf("What is your last name? ");

scanf(" %s", last); /* No apersand on character strings */



printf("How old are you? ");

scanf(" %d", &age); /* Ampersand required */

printf("How do you weight? ");

scanf(" %f", &weight); /* Ampersand required
*/



printf("\nHere is the information you entered:\n");

printf("Name: %s %s\n", first, last);

printf("Weight: %3.0f\n", weight);

printf("Age: %d", age);



if (age < 18)

{ printf("You cannnot vote yet\n");

yrs = 18 - age;

printf("You can vote in d% years.\n",
yrs);

}

else

{

printf("You can vote.\n);

}



return 0; /* Always best to do this */

}
 
S

Shani

Your code looks fine excepts for few major mistake.
1) Never use a variable without declaring it you have not declare
yrs in your code anyhwere.
2) Always declare and initialize your variables it will save you lot of
headic.
3) You are missing a " in your last PRINTF statement try
printf("You can vote.\n")
4) Lastly you are missing a curley brace at the end of your program.

Try this program below



#include <stdio.h>


main()


{
//Declaring and initializing variables
int age = int();
double yrs = double();
float weight = float();

char first[15], last[15]; /* 2 char arrays */
printf("\nWhat is your first name? ");
scanf(" %s", first); /* No ampersand on
character strings */
printf("What is your last name? ");
scanf(" %s", last); /* No apersand on character strings
*/
printf("How old are you? ");
scanf(" %d", &age); /* Ampersand
required */
printf("How do you weight? ");
scanf(" %f", &weight); /* Ampersand
required */
printf("\nHere is the information you entered:\n");
printf("Name: %s %s\n", first, last);
printf("Weight: %3.0f\n", weight);
printf("Age: %d", age);

if (age < 18)


{ printf("You cannnot vote yet\n");


yrs = 18 - age;


printf("You can vote in d% years.\n",
yrs);


}


else


{


printf("You can vote.\n"); //You
missed a double quote


}


return 0; /* Always best to do this */


} // you missed this brace
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top