468,512 Members | 1,626 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,512 developers. It's quick & easy.

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

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 */

}


Jul 23 '05 #1
1 1071
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

Jul 23 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

reply views Thread by vishnu mahendra | last post: by
1 post views Thread by Serdar C. | last post: by
6 posts views Thread by peter_k | last post: by
17 posts views Thread by Brian Blais | last post: by
12 posts views Thread by Steve Howell | last post: by
17 posts views Thread by allen.fowler | last post: by
reply views Thread by NPC403 | last post: by
1 post views Thread by fmendoza | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.