home hardware prices news articles forums photos user reviews
Go Back   Tech Support Forums - TechIMO.com > PC Hardware and Tech > Webmastering and Programming
Ask a Tech Support Question (free)!

C++ Resetting variables

Reply
Get bargains at  »  Dealighted.com
 
Thread Tools Search this Thread
Currently Active Users: 1566
Discussions: 200,933, Posts: 2,379,174, Members: 246,297
Old October 29th, 2004, 08:06 PM   Digg it!   #1 (permalink)
Senior Member
 
TechKnickle's Avatar
 
Join Date: Aug 2004
Location: LA, California
Posts: 812
Send a message via AIM to TechKnickle Send a message via MSN to TechKnickle
C++ Resetting variables

Help please

Code:
//Spell Poncho
//By me

#include <iostream>
#include <string>

int main ()
{
char one, two, three, four, five, answer, poncho, p, six,o,n,c,h;
int i;
i=1;

{
  
   std::cout << "\t\t-----------------------------------------------\n";
   std::cout << "\t\t-           Spell Poncho Version 1.0          -\n";
   std::cout << "\t\t-           How do you spell Poncho?          -\n";
   std::cout << "\t\t-             SPELL PONCHO CORRECT            -\n";
   std::cout << "\t\t-             TO CLOSE THE PROGRAM            -\n";
   std::cout << "\t\t-----------------------------------------------\n";
   std::cout << "\n";
   std::cout << "\n";
      

  while (i ==1)
{
{



std::cout << "SPELL PONCHO: ";
std::cin >> one;
std::cin >> two;
std::cin >> three;
std::cin >> four;
std::cin >> five;
std::cin >> six;

}
if (six != 'o' or five !='h' or one !='p' or two !='o' or three !='n' or four !='c')
 
{
    std::cout << "Spelling PONCHO\n";
    std::cout << "\n";
    std::cout << "\n";
    std::cout << "WRONG SPELLING DETECTED!!!  IT IS SPELLED PONCHO YOU IDIOT!";
    std::cout << "\n";
    std::cout << "\n";
    std::cout <<"You Spelled: " << one<<two<<three<<four<<five<<six;
    std::cout << "\n";
    std::cout << "\n";
    std::cout << "TRY SPELLING IT AGAIN!";
    std::cout << "\n";
    std::cout << "\n";
    std::cout << "\n";

}
if (six =='o' and five =='h' and one =='p' and two =='o' and three =='n' and four =='c')
{

    std::cout << "\n";
    std::cout << "\n";
    std::cout << "\nGreat job!  You spelled: ";
    std::cout << one<<two<<three<<four<<five<<six;
    std::cout << "\n";
    std::cout << "\n";


}    
}
} 
}
Here is my source.

I need to know how to reset the variables one,two,three,four,five,six because if they misspell poncho one time then every other time poncho will be misspelled no matter what happens because the variables are not cleared.

Can someone tell me how to clear/reset these variables?
__________________
People are like coins, there's always two sides.

Last edited by TechKnickle : October 29th, 2004 at 08:07 PM. Reason: Fix title
TechKnickle is offline   Reply With Quote
Old October 29th, 2004, 09:02 PM     #2 (permalink)
Ultimate Member
 
elmers's Avatar
 
Join Date: Sep 2003
Location: Philadelphia
Posts: 1,484
The problem is not in the one two ... its in cin. If you type "1234567890" and then do cin into a char 6 times you get "7890" still left in the buffer. If you then type poncho the program reads what it missed the last time meaning "7890po" and you get "ncho" for the next read and so on. You should step thru the code to see exactly whats happening.

Your code isnt robust for all input. You need to use a string or clear the cin buffer.

By the way afaik you use "one = 'x';" to assign values if thats what your asking.

#include <iostream>
#include <string>
using namespace std; // will use cout instead of std::cout ..

int main ()
{
string input_word;
string wanted_word = "poncho";
{
cout << "\t\t-How do you spell " << wanted_word << "-\n";
while (1)
{
cout << "SPELL " << wanted_word << "\n";
cin >> input_word;
input_word;

if ( input_word == wanted_word){
cout <<"Great job! You spelled: \n"<< input_word << "\n";
}

else{
cout << "\nYou made a mistake! You spelled: "<< input_word<< "\n";
}
}
}
}

Last edited by elmers : October 29th, 2004 at 10:33 PM.
elmers is offline   Reply With Quote
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search

Similar Threads
Thread Thread Starter Forum Replies Last Post
PHP Variable split help. Immortal55 Webmastering and Programming 0 July 11th, 2004 02:48 AM
VB Variables to DOS variable Pritesh Webmastering and Programming 4 October 25th, 2003 09:53 AM
win xp temp file freem Technical Support 4 December 12th, 2002 10:36 AM
I need some Visual Basic code Bob The Great Webmastering and Programming 45 April 29th, 2002 12:13 AM
Q3a question Donkey General Gaming Discussion 6 January 14th, 2002 04:25 PM


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Most Active Discussions
Is It Just Me? (2878)
Obama the Muslim (14)
California Passes Anti-Flat-HDTV Le.. (39)
Is the PSU I received dead? (10)
windows vista security holes (9)
Install XP pro and a Vista laptop ?.. (11)
HIS HD5770 graphic card question (15)
Print spooler problem (13)
Foreign voltage (10)
Dept. of HS: NSA 'Helped' Develop V.. (15)
A good PSU? (10)
EVGA 9800 gtx help with finding a g.. (8)
New Computer wont recognize XP disc (7)
Ideal cheap graph card for PC-Gamin.. (17)
Recent Discussions
Wireless Televisions. (1)
Ideal cheap graph card for PC-Gaming? (17)
CPU fan stops spinning randomly (0)
BIOS won't read disk when I try to fl.. (0)
Install XP pro and a Vista laptop ?? (11)
Partition Magic caused HDD problem (2)
Graphics Card Upgrade Question (1)
favorit (1)
solutions for virtical white lines on.. (1)
Regular Build (3)
Fire in DVD (2)
Modern Warfare For the PC (33)
radeon x850xt platinum & shader 3 (3)
Have you switched yet? (84)
Wireless Router+Cable Modems and Much.. (0)
Optical Audio A-B Switch (1)
windows vista security holes (9)
The NTDVM CPU has encountered an ille.. (24)
[F@H SPAM 11/16/09] ! 1/2 months to r.. (34)
Wireless speakers for PC? (11)
Print spooler problem (13)
Help getting around port 80 for camer.. (2)
Display shows 3x5 inch in middle of s.. (3)
monitor will not turn on at all, (1)
World's largest Monopoly Game using G.. (331)


All times are GMT -4. The time now is 09:01 AM.
TechIMO Copyright 2009 All Enthusiast, Inc.



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28