Thread: Any Room For Improvement?

  1. #1
    Registered User
    Join Date
    Sep 2009
    Posts
    13

    Any Room For Improvement?

    I've been studying C for sometime now and Ive started with the standard library's of course, but anyway im trying to make sure to keep my code organized and neat. Main question is I know when making computations for big projects its important to use functions so we can have easy access if you need to update it, but its not necessary in a small project right? Im trying to organize my code 100% so any comments are great! Thanks.

    Code:
    // Client.cpp : Defines the entry point for the console application.
    //
    
    #include "stdafx.h"
    #include "Client.h" 
    
    int main()
    {
    
    	std::cout<<"What is your name?\n";
    	std::cin>>client.name;
    	std::cout<<"Your name is: "<<client.name<<"\n";
    	std::cout<<"How many records will we be computing today?"<<std::endl;
    	std::cin>>records.number;
    	for (int i = 1; i <= records.number; ++i) // function maybe? we may want to call this a 100 more times in the future so we want easy access to it.
    	{
    	std::cin>>records.localnumber;
    	records.value += records.localnumber; // lol I hope my logic is good here lawl. :|
    	std::cout<<"Client records processed: "<<records.value<<"\n";
    	}
    
    }
    Code:
    // Client.h
    
    #pragma once 
    
    struct ClientStats_t // data structure
    {
    	int number; //data members
    	int localnumber;
    	int value;
    	std::string name;
    } client, records; // data object
    
    //ClientStats_t client; //a second header was causeing a conflict make sure you only include the struct once. or you will confuse the compiler.
    Code:
    // stdafx.h
    
    #include <string>
    #include <iostream>
    #include <stdio.h>
    #include <tchar.h>
    #include "targetver.h"
    
    // TODO: reference additional headers your program requires here
    Last edited by Clinthill98; 03-31-2010 at 09:06 PM.

  2. #2
    Registered User rogster001's Avatar
    Join Date
    Aug 2006
    Location
    Liverpool UK
    Posts
    1,472
    well like you say, it is small and is like a code snippet example so why bother with a function?..because in my opinion its a neater solution and clearly compartmentalises the role of different code sections, and of course its good practice for you for the future.

    i prefer to indent for loop content to make it clearer to read

  3. #3
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    1. do not use global vars
    2. check return value of input operations - if user instead of number inputs string - you do not hve number in your var and your input stream is in a failed state - so all other input operations will fail as well till you flush the stream and reset its state to normal
    3. use setters and getters for the members of your class, make vars - private - and check data before assigning it to members
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Moving between areas
    By Aliaks in forum C++ Programming
    Replies: 100
    Last Post: 07-27-2009, 02:23 AM
  2. Got a project. No help, confused on how to do this.
    By DazedNconfused in forum C# Programming
    Replies: 5
    Last Post: 10-12-2008, 07:02 AM
  3. Handling movement in text-adventures.
    By suzakugaiden in forum Game Programming
    Replies: 31
    Last Post: 02-13-2005, 03:18 AM
  4. MUD Concept Question
    By mrpickle in forum Game Programming
    Replies: 3
    Last Post: 12-01-2003, 12:45 PM
  5. Pathfinding AI? (Not the A* Algorithim)
    By harryP in forum C++ Programming
    Replies: 22
    Last Post: 08-01-2003, 02:32 PM