• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
AndrewBlalock

C Program

6 posts in this topic

I am working on a project for my C programming class and I have the whole program done. Now we have just started the class so we dont know much but I do have some other programming experience. I get the program to run through the first time with a selection but the it will display the info once more but wont let the user selection another option after the first time it jumps right to another function and I am not sure why. I have changed stuff around and tried to figure it out but I can so if someone could point me in the right direction or give me a hint anything would be helpful thanks.

[code]

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

#define SALES_TAX .07
#define INCHES_IN_FEET 12
#define COVERAGE_BAG 2

void display();
char entry();
float mulchBed();
float numberOfBags(float area);

int main()
{
printf("welcome to Andrew's Home Improvement Store!\n\n");
// variables for different types of mulch using parallel arrays
float mulchPriceOne = 3.97;
float mulchPriceTwo = 4.97;
float mulchPriceThree = 2.25;

float area; // the area of the mulch bed
float bags; // number of bags of mulch
float mulchPrice;
float price;
float tax;
float totalPrice;

char mulchType;
mulchType = entry();
while( mulchType != 'Q')
{

if(mulchType == 'E')
{
printf("You choose the Earth Gro mulch\n.");
mulchPrice = mulchPriceOne;
}
else if(mulchType == 'N')
{
printf("You choose the Nature Scapes mulch\n.");
mulchPrice = mulchPriceTwo;
}
else if(mulchType == 'G')
{
printf("You choose the Gardens Pro mulch\n.");
mulchPrice = mulchPriceThree;
}

area = mulchBed();
bags = numberOfBags(area);
price = mulchPrice * bags;
tax = price * SALES_TAX;
totalPrice = price + tax;


printf("The area of your mulch bed is %.0f square feet\n", area);
printf("The number of bags you will need %.2f bags\n", bags);
printf("The price of your mulch will be $%.2f\n", price);
printf("The tax on your bags will be $%.2f\n", tax);
printf("The total price including sales tax will be $%.2f\n\n\n", totalPrice);

mulchType = entry();
}


printf("Results were provided by Andrew Blalock")
return 0;

}
void display()
{
printf("Mulch\t\t\tSq Footage\t\t\tPrice Sq Ft\n\n");
printf("Earth Gro\t\t\t2 cu ft\t\t\t$3.97\n");
printf("Nature Scapes\t\t\t2 cu ft\t\t\t$4.97\n");
printf("Gardens Pro\t\t\t2 cu ft\t\t\t$2.25\n\n\n\n");
}
char entry()
{
char selection;
display();
printf("Choose what type of mulch you wish to purchase or exit the program.\n");
printf("(E)Earth Gro\n(N) Nature Scapes\n(G)Garden Pro\n(Q)Quit\n");
scanf("%c", &selection);
return selection;

}

float mulchBed()
{
int length;
int width;
int depthInches;
float depthFeet;

printf("What the is length of you mulch bed going to be(in feet):");
scanf("%i", &length);
printf("What the is width of you mulch bed going to be(in feet):");
scanf("%i", &width);
printf("What the is depth of you mulch bed going to be(in inches):");
scanf("%i", &depthInches);

depthFeet = depthInches / INCHES_IN_FEET;

float area;
area = length * width * depthFeet;
return area;

}

float numberOfBags(float area)
{
float bags;
bags = area / COVERAGE_BAG;
return bags;

}
[/code]
0

Share this post


Link to post
Share on other sites
What IDE are you using ?

If it's Visual Studio 2010 etc....try setting some break points and step through the code to see what it is doing.
0

Share this post


Link to post
Share on other sites
When a scanf conversion string starts with "%c", it doesn't skip over white space. Upon pressing the Enter key in a Windows console, 2 characters are in fact generated -- a '\r' and a '\n'. I suspect the '\n' is converted by the scanf() call inside entry() at the end of your primary while-loop.

The easiest fix is to change 'scanf("%c", &selection);' to 'scanf(" %c", &selection);' (note the extra space).

Or you can explicitly skip over white space inside entry() by use fgetc() and isspace() in a loop.
-1

Share this post


Link to post
Share on other sites
`scanf ("%c",...)' will read any characters, including spaces and end-of-line characters. The first time you run your program, there is nothing to be read, and it works as you expect. But after you have called `scanf("%i",...)', the end-of-line character is left in the buffer, and the next call to `scanf("%c",...)' reads it. You can patch your program with something that will consume spaces and end-of-line characters before reading the character you want, but it's sort of a kludge.

Console input is notoriously hard to get right in C. My preferred solution is to always read a complete line of input (using something like fgets), and then parsing what you just read (using something like sscanf). It's a bit of work and it might be too complicated for you at this stage, if you still don't know how to use strings. But it's about the only solution that doesn't suffer from the type of problem you are experiencing.
2

Share this post


Link to post
Share on other sites
[quote name='alvaro' timestamp='1306346333' post='4815689']
Console input is notoriously hard to get right in C.[/quote]

I'd have to agree, given that my answer was incorrect -- if I could down-vote myself, I would! Just wanted to mention explicitly that the confusing half-truth I posted should be ignored.
0

Share this post


Link to post
Share on other sites
getch();

EDIT: The above was my post, and it solves all the issues the OP had. I didn't feel there needed to be more to add, but for for clarity. Just call:
[code]
selection = getch();
[/code]
in entry()
-2

Share this post


Link to post
Share on other sites
[quote name='edd²' timestamp='1306358731' post='4815777']
[quote name='alvaro' timestamp='1306346333' post='4815689']
Console input is notoriously hard to get right in C.[/quote]

I'd have to agree, given that my answer was incorrect -- if I could down-vote myself, I would! Just wanted to mention explicitly that the confusing half-truth I posted should be ignored.
[/quote]
Just wondering why this particular post was downvoted?!
1

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0