Thread: C++ Need Help writing a Small program

  1. #1
    Registered User
    Join Date
    Sep 2008
    Posts
    37

    Question C++ Need Help writing a Small program

    Wavelenght of visible light fall approximately into the range 0.4--0.7 microns. The Table below shows the relationship between wavelenght and color.

    Write a program that inputs a wavelenght and then displays the associated light color. If the wavelenght is shorter than 0.4 microns or longer than 0.7, display the message wavelenght outside visual range.
    clasiffy boundary wavelenghts as the lower-wavelenght color.
    For example, label a wavelenght of 0.424 microns as violet.

    Color_________________________________Aproximate wavelenght range(microns)
    ------------------------------------------------------------------------------------------------------------
    Violet________________________________________0.40 0--0.424
    Blue_________________________________________0.424--0.491
    Green_______________________________________0.491--0.575
    Yellow_______________________________________0.575--0.585
    Orange______________________________________0.585--0.647
    Red_________________________________________0.647--0.700




    I tried it but with no success, i had like 12 errors.

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    And those 12 errors are?

  3. #3
    Registered User
    Join Date
    Sep 2008
    Posts
    37
    I don't know exactly where I'm wrong, I'm lost I'm a beginner. can you Please Post the Write Code. Thanks



    Code:
    //robert
    include <iostream>
    using namespace std;
    
    Int main (void) {
    
    float w;
    cout << "Input Wavelenght " ;
    cin >> w ;
    
    If (w > .7) {
    cout " wavelenght outside visual range " ;
    
    } else if (w > .647 ) {
    cout << " red " ;
    
    } else if (w > .585 ) {
    cout << " orange " ;
    
    
    } else if (w > .575 ) {
    cout << " yellow " ;
    
    } else if (w > .491 ) {
    cout << " green " ;
    
    } else if (w > .424 ) {
    cout << " blue " ;
    
    } else if (w > .400 ) {
    cout << " violet " ;
    
    
    }else{
    
    cout<< " out of range " ;
    
    return 0 ;
    
    }

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    You need to put code inside a main() function.

  5. #5
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    I immediately see 2 very simple syntactical errors on the second and fifth line. Look up a Hello World example and compare it to your code, remembering that C++ is case-sensitive.

    tabstop: The code is inside a main - but with the lack of indentation, that's easy to miss.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  6. #6
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    To help you out a bit (no, I'm not going to write your code).
    Error lines marked in red.
    Code:
    //robert
    include <iostream>
    using namespace std;
    
    Int main (void) {
    
    float w;
    cout << "Input Wavelenght " ;
    cin >> w ;
    
    If (w > .7) {
    cout " wavelenght outside visual range " ;
    
    } else if (w > .647 ) {
    cout << " red " ;
    
    } else if (w > .585 ) {
    cout << " orange " ;
    
    
    } else if (w > .575 ) {
    cout << " yellow " ;
    
    } else if (w > .491 ) {
    cout << " green " ;
    
    } else if (w > .424 ) {
    cout << " blue " ;
    
    } else if (w > .400 ) {
    cout << " violet " ;
    
    
    }else{
    
    cout<< " out of range " ;
    
    return 0 ;
    
    }
    Now you figure out the problems.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  7. #7
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Indeed it is -- I managed to completely miss that the first time. "If" is also a problem.

  8. #8
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    And something the compiler won't catch: it's spelled "wavelength".
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Client-server system with input from separate program
    By robot-ic in forum Networking/Device Communication
    Replies: 3
    Last Post: 01-16-2009, 03:30 PM
  2. Writing a Unix Search Program, some questions
    By Acolyte in forum C Programming
    Replies: 3
    Last Post: 09-23-2008, 12:53 AM
  3. Need some help with C program writing
    By The_PC_Gamer in forum C Programming
    Replies: 9
    Last Post: 02-12-2008, 09:12 PM
  4. Writing a C program for class....
    By Bizmark in forum C Programming
    Replies: 10
    Last Post: 11-13-2007, 10:31 AM
  5. Writing a checklist program for MTGO?
    By CrYpTiC in forum Game Programming
    Replies: 2
    Last Post: 04-08-2005, 09:46 PM